Identify a websocket from an ExpressJS request - node.js

How can one use an Express POST request as the basis for a socket.io broadcast (instead of a socket message)?
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
app.post('/campaigns', function(req, res, next) {
var campaign = new Campaign(req.body);
campaign.save(function(err, campaign) {
if (err) {
next(err);
} else {
res.json(campaign);
// how to broadcast a message to everyone
// except the sender of this POST request?
io.of('/campaigns').emit('new', campaign);
}
});
});

Very simple.
Each time you establish a connection to server, your socket object will have id associated with this connection. Save it on client side in some variable.
Then each time you post a request to server add socket id to query parameters. Upon receiving POST request on server side broadcast the message to all users and socket id to message. On client side modify on message event by adding a logic to skip/hide received message if client's socket id is equal to socket id received as a extra parameter in message.
Pitfalls
May not work if for some reason after POST request client was disconnected and reconnected to server - in this case upon receiving a message back client will have new socket id that is different from what it had before sending POST request. In that case you will have to come up with some more connection-independent client ID scheme to identify client and use it instead of socket.id as identifier. You may try to use sessions.
Client will still receive message even if it won't be displayed.
Not good to combine client-server "intercommunication" methods and use one from another and vice versa.

Related

How to connect clients with a preset ID on a socket server?

My setup looks like this:
Client --> AuthenticationService --> REST-API --> MessageBroker(WebSocket) --> Client(s)
When a client is authenticated in the system, it gets a token ID. Now, when any client is changing a value via the REST-API, I want to push this change through websockets to every client BUT the one who changed it.
Therefore I want to filter my socket.clients[] through their token IDs.
When I transmit the change, it is easy: I'll just send the token-ID until the REST-API.
But I need to somehow connect to the socket with the exact same
tokenID. How can I accomplish this? Any best practices here?
That's the test client code:
(function() {
const ws = new WebSocket('ws://localhost:8080');
const id = 123123
ws.onopen = () => {
console.log('websocket is connected ...')
// sending the id to the socket after connecting
ws.send('connected', id)
}
})
But now the problem is: How do I know on the socket which message is meant to transport the id and which are the just normal messages?
I don't want to check EVERY message from the client and see if it's an
id message.
Any help here? What's a good practice to connect clients with a socket with a preset ID?
To answer my own question:
When the client is connecting to the websocket, just pass a parameter to the url:
const webSocket = new WebSocket('ws://127.0.0.1:8080?tokenID=123')
In the backend you can get the tokenID via url.parse:
const id = url.parse(req.url, true).query.tokenID

Websockets & NodeJS - Changing Browser Tabs & Sessions

I've started writing a node.js websocket solution using socket.io.
The browsers connects to the node server successfully and I get see the socket.id and all config associated with console.log(socket). I also pass a userid back with the initial connection and can see this on the server side to.
Question: I'm not sure the best way to associate a user with a connection. I can see the socket.id changes every page change and when a tab is opened up. How can I track a user and send 'a message' to all required sockets. (Could be one page or could be 3 tabs etc).
I tried to have a look at 'express-socket.io-session' but I'm unsure how to code for it and this situation.
Question: I have 'io' and 'app' variables below. Is it possible to use the 2 together? app.use(io);
Essentially I want to be able to track users (I guess by session - but unsure of how to handle different socket id's for tabs etc) and know how to reply to user or one or more sockets.
thankyou
The best way to handle the situation is rely on SocketIO's rooms. Name the room after the user's unique ID. This will support multiple connections out of the box. Then, whenever you need to communicate with a particular user, simply call the message function and pass in their id, the event, and any relevant data. You don't need to worry about explicitly leaving a room, SocketIO does that for you whenever their session times out or they close their browser tab. (We do explicitly leave a room whenever they log out though obviously)
On the server:
var express = require('express');
var socketio = require('socket.io');
var app = express();
var server = http.createServer(app);
var io = socketio(server);
io.on('connect', function (socket) {
socket.on('userConnected', socket.join); // Client sends userId
socket.on('userDisconnected', socket.leave); // Cliend sends userId
});
// Export this function to be used throughout the server
function message (userId, event, data) {
io.sockets.to(userId).emit(event, data);
}
On the client:
var socket = io('http://localhost:9000'); // Server endpoint
socket.on('connect', connectUser);
socket.on('message', function (data) {
console.log(data);
});
// Call whenever a user logs in or is already authenticated
function connectUser () {
var userId = ... // Retrieve userId somehow
if (!userId) return;
socket.emit('userConnected', userId);
}
// Call whenever a user disconnects
function disconnectUser () {
var userId = ... // Retrieve userId somehow
if (!userId) return;
socket.emit('userDisconnected', userId);
}

How to send message to a specific client with socket.io if the application uses the cluster up and running in several processes on different ports?

The application starts in cluster mode, each worker is to establish a connection to the socket, using redis adapter:
app.set('port', httpPort);
let server = http.createServer(app);
let io = require('./socketServer')(server);
io.adapter(redis({host: host, port: port}));
app.set('io', io);
then we connect the main socket.io file (socketServer), where after authorization of the socket and on.connection event, we save sessionID in variable socketID, and store current socket connection in array io.clients
io.sockets.on('connection', (socket) =>{
var socketID = socket.handshake.user.sid;
io.clients[socketID] = socket;
io.clients[socketID].broadcast.emit('loggedIn',socket.handshake.user.data);
socket.on('disconnect', () =>{
delete io.clients[socketID];
});
});
Before nodejs app, we have nginx with customized "upstream" to organize a "sticky sessions" (http://socket.io/docs/using-multiple-nodes/#nginx-configuration).
Then, when we want to send a message to a particular customer, already from the controller we get id user, and get session-id for id (we pre-authorization keep these correspondences in redis), and then just send a message:
this.redis.getByMask(`sid_clients:*`,(err,rdbData) =>{
Async.each(clients,(client,next)=>{
let sid = `sid_clients:${client}`;
let currentClient = rdbData[sid];
if(!currentClient || !this.io.clients[currentClient]) return next();
this.io.clients[currentClient].emit(event,data);
return next();
});
It works fine when we run the application in a single process. But this don't work when running in a cluster mode. Connection message "loggedIn" is send to all customers on all processes. But if a single process to send a message to the client that connects to a server in another process - does not work, because that each process has own array io.clients and they are always have different content, so the message does not can reach the right customer.
So, how send events to the specific client in a cluster mode? How to keep all connected sockets in one place to avoid situations such as mine?

Reconnect socket in disconnect event

I am trying to reconnecct the socket after the disconnect event is fired with same socket.id here is my socket config
var http = require('http').Server(app);
var io = require('socket.io')(http);
var connect_clients = [] //here would be the list of socket.id of connected users
http.listen(3000, function () {
console.log('listening on *:3000');
});
So on disconnect event i want to reconnect the disconnected user with same socket.id if possible
socket.on('disconnect',function(){
var disconnect_id = socket.id; //i want reconnect the users here
});
By default, Socket.IO does not have a server-side logic for reconnecting. Which means each time a client wants to connect, a new socket object is created, thus it has a new id. It's up to you to implement reconnection.
In order to do so, you will need a way to store something for this user. If you have any kind of authentication (passport for example) - using socket.request you will have the initial HTTP request fired before the upgrade happened. So from there, you can have all kind of cookies and data already stored.
If you don't want to store anything in cookies, the easiest thing to do is send back to client specific information about himself, on connect. Then, when user tries to reconnect, send this information again. Something like:
var client2socket = {};
io.on('connect', function(socket) {
var uid = Math.random(); // some really unique id :)
client2socket[uid] = socket;
socket.on('authenticate', function(userID) {
delete client2socket[uid]; // remove the "new" socket
client2socket[userID] = socket; // replace "old" socket
});
});
Keep in mind this is just a sample and you need to implement something a little bit better :) Maybe send the information as a request param, or store it another way - whatever works for you.

How to send messages by socket.io to specific logged in users after certain events?

I take a few step into socket.io. It's very hard to code without req, because I don't know response to who.
For this question. My website have some blogs. When the blogs updated , I need to 'tell' users who followed the blogs that the blogs have been updated. The problems are I don't know witch followed users are online and I don't know how to emit events to them with only followed user id.
Can some one give me some tips? No need any specific code.
You can call join to subscribe the socket to a given channel:
io.on('connection', function(socket){
socket.join('some room');
});
And then simply use to when broadcasting or emitting:
io.to('some room').emit('some event');
To leave a channel, you need to call leave :
io.on('disconnect', function(socket){
socket.leave('some room');
});
You can make use of cookies to map http requests to corresponding socket connection. Lets say we are using cookie sesson-id.
Drop this cookie on client on a http request.
In the following patch of code, before creating a new socket connection, you attach session-id cookie to the socket header. This way you can access this session-id in .on("connection") event:
var io = require("socket.io");
sio = io.listen(server);
sio.set("authorization", function (data, accept) {
if (data.headers.cookie) {
data.cookie = cookie.parse(data.headers.cookie);
data.headers.sessionID = data.cookie["session-id"];
}
accept(null, true);
});
Next, you store socket connection objects associated to their session-id:
var sockets = {}; // store all socket connections here
sio.sockets.on("connection",connected);
function connected(socket){
var sessionID = socket.handshake.headers.sessionID;
sockets[sessionID] = socket;
}
Now, whenever you wish to emit socket event related to a particular http request:
function dummyReq(req,res){
var session_id = // get cookie from req
//after certain actions
sockets[session_id].emit("dummy-event","dummy-message");
}
In your case you will have to emit socket events for all session-id linked to users who are following those blogs

Resources