Socket IO room

PHOTO EMBED

Thu Aug 19 2021 18:45:47 GMT+0000 (Coordinated Universal Time)

Saved by @ExplodeMilk #typescript #express #socketio

app.post('/apple',(req,res)=>{
    // logic of adding apple.
    if(req.session){
        io.to(`user-${req.session['user'].id}`).emit("new-apple","Congratulations! New Apple Created!");
    }
    res.json({updated:1});
});

io.on('connection', function (socket) {
    ....
    if(socket.request.session['user']){
        socket.join(`user-${socket.request.session['user'].id}`);  
        // One common way is to join the socket to a room named by the `user.id` or other group information.
     }
});
content_copyCOPY