Wednesday, 10 May 2017

websocket chat rooms with node.js

have a module with "websocket.server":

const WebSocketServer = require('ws').Server;
wsServer = module.exports =  {};

wsServer.init = (server) => {
    wsServer.ws = new WebSocketServer({server: server});

    wsServer.ws.on('connection', function msg_connection(ws) {
        ws.on('message', function msg_incoming(message) {

        });
    });
};

and "sender function" to all users that join my url like "site.com/chat/1":

//require websocket server
var messageSocket = require('../websocket/websocketServer');

messageSocket.ws.clients.forEach((client) => {
  client.send(JSON.stringify({
    type: 'text',
    message: messageText
  }));
});

it works fine, all users recieve their messages, but the problem that i have others links with other chats, like "site.com/chat/2" and "site.com/chat/3", how i can separate chats? mean make this chats as "rooms"



via 20yco

No comments:

Post a Comment