Socket

  • ​​You can use socket.io to configure socket
  • Run npm i socket.io socket.io-client in the terminal
  • Example
my-nodestack-app
├── src
│   ├── pages
│   │   │── socket      
│   │   │   │── index.js
│   ├── plugins
│   │   │── socket.js    
├── ndsk.config.js
└── package.json
const { Server } = require("socket.io");
 exports.plugin  = {
     name: 'socket',
     register: async (server) =>{
         const io = new Server(server.listener,{
             path:"/socket"
         });
         io.use((socket, next) => {
             // ..code
             next()
         });

         io.on("connection", (socket) => {
             socket.emit('status','Socket connection successful')
         });
     }
 }
 import { useState,useEffect } from "react";
 import { io } from "socket.io-client";
 export default (props)=>{  
     const [status,setStatus] = useState('Waiting')
     useEffect(() => {
         const socket = io.connect("ws://localhost:3000", {
             path:'/socket', 
             transports: ['websocket']
         });
         socket.on('status',data=>{
             setStatus(data)
         })
     },[]);
     return (
         <div>{status}</div>
     );
 }

Next

Use with pm2