I am trying to use sockets with node.js, I succeded but I don't know how to differentiate clients in my code. The part concerning sockets is this:
var WebSocketServer = require('ws').Server, wss = new WebSocketServer({port: 8080}); wss.on('connection', function(ws) { ws.on('message', function(message) { console.log('received: %s', message); ws.send(message); }); ws.send('something'); });
This code works fine with my client js.
But I would like to send a message to a particular user or all users having sockets open on my server.
In my case I send a message as a client and I receive a response but the others user show nothing.
I would like for example user1 sends a message to the server via webSocket and I send a notification to user2 who has his socket open.
WebSocket Client in Node.js A WebSocket connection has two components, a client and a server. In the above example, you created a server. Clients initiate a request to open a WebSocket connection, and servers respond to inbound requests to open WebSocket connections.
A WebSocket connection has two components, a client and a server. In the above example, you created a server. Clients initiate a request to open a WebSocket connection, and servers respond to inbound requests to open WebSocket connections. You can also create a WebSocket client in Node.js using ws.
You can use Redis pub-sub and make a "publish" channel which every server you have listens to. Whenever a message is published on that channel, every server broadcasts to all of their clients. Simple as that. Per the docs, WebSocketServer has a clients property that tracks current clients for you.
Unlike HTTP servers, WebSockets ones don’t have any routes by default because it is just not needed. In this protocol, you just use a string to send and receive information (a good practice is to send a JSON object serialized to a string). That’s why the configuration is so simple, still, it’s not the end.
In nodejs you can directly modify the ws client and add custom attributes for each client separately. Also you have a global variable wss.clients and can be used anywhere. Please try the next code and try to connect at leat two clients:
var WebSocketServer = require('ws').Server; var wss = new WebSocketServer({ server: httpsServer }); wss.getUniqueID = function () { function s4() { return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1); } return s4() + s4() + '-' + s4(); }; wss.on('connection', function connection(ws, req) { ws.id = wss.getUniqueID(); wss.clients.forEach(function each(client) { console.log('Client.ID: ' + client.id); }); });
You can also pass parameters directly in the client connection URL:
https://myhost:8080?myCustomParam=1111&myCustomID=2222
In the connection function you can get these parameters and to assign these parameters directly to your ws client:
wss.on('connection', function connection(ws, req) { const parameters = url.parse(req.url, true); ws.uid = wss.getUniqueID(); ws.chatRoom = {uid: parameters.query.myCustomID}; ws.hereMyCustomParameter = parameters.query.myCustomParam; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With