Skip to content Skip to sidebar Skip to footer

Socket.io Best Way To Send Messages Between Two Users?

I want to know what is the best way to send messages between two users? I know you can do rooms and join them but you have to 'create' them first. Think of it like chat messenger.

Solution 1:

Every socket in Socket.io has its own ID. You can send messages directly to a socket, once you know that ID. Example from https://socket.io/docs/v3/rooms/index.html#Default-room

    io.on('connection', socket => {
        socket.on('private message', (anotherSocketId, msg) => {
            socket.to(anotherSocketId).emit('private message', socket.id, msg);
        });
    });

Post a Comment for "Socket.io Best Way To Send Messages Between Two Users?"