Nodejs, clients not always receiving server messages - javascript

I am setting up a very basic node application, the plan is that anyone can go on the site and press a letter and that will increment a counter. Everybody can then see that counter going up as people all over the world are incrementing it.
It works brilliantly, except that the way i set it up in the beginning then the node server is contacting each client 20 times per second, which is very wasteful.
So in order to avoid that i added a condition that the counter must have increased in order for the node server to send the message. But when I do that the messages never really make it to all the clients, how they go to the clients is some confusing, apparently random order.
This is my first node app and I am still getting the hang of it, can anybody point me in the right direction with this? The problem is probably in the increment_time function.
Server part:
var http=require('http');
var io=require('socket.io');
var fs=require('fs');
var sockFile=fs.readFileSync('text');
server=http.createServer();
//2. get request, via port, http://localhost:8000
server.on('request', function(req, res){
console.log('request received');
res.writeHead(200, {'content-type':'text/html'});
res.end(sockFile);
});
//have server and socket listen to port 8000.
server.listen(8000);
var socket=io.listen(server);
//the lower the number the less chatty the debug becomes.
socket.set('log level', 1);
//initiate the counter.
counter=0;
counterp=0;
//3. on client connect.
//http://<address>/apps/press_k
socket.on('connection', function(socket){
console.log("Client connected.");
//increment counter on clients command.
socket.on('s_increment_counter',function(data){
counter++;
console.log('New counter:'+counter);
});
//send the counter to all clients if it has increased.
increment_time_continuously();
function increment_time(){
//this is the condition that is to limit how often node sends to the clients,
//but setting it on means the counter will be delivered in some erratic way
//to the clients.
//if(counter===counterp) {return;}
//send current counter to all clients.
console.log("passed the equals, now broadcast.");
//this part is clearly only sending to one of them.
socket.emit('c_display_counter', counter);
//client.broadcast.emit('c_display_counter', counter)
//client.send('Welcome client');
counterp=counter;
}
function increment_time_continuously(){setInterval(increment_time,50);}
});
Relevant client part:
//client_to_server:
//-tell server to increment counter.
function s_increment_counter(){
socket.emit('s_increment_counter',{counter:0});
}
//server_to_client:
//-when server tells to display counter, display counter.
socket.on('c_display_counter',function(counter){
//counter=data["counter"];
k_counter_text.attr({"text":counter});
});

The problem is that you dont store your connections and when you broadcast with socket.emit it goes only to your last connection, you need to store each connection (in a array or something) that has connected to the socket and then iterate over all your connections and broadcast the message you want.
var http=require('http');
var io=require('socket.io');
var fs=require('fs');
var sockFile=fs.readFileSync('text');
server=http.createServer();
//2. get request, via port, http://localhost:8000
server.on('request', function(req, res){
console.log('request received');
res.writeHead(200, {'content-type':'text/html'});
res.end(sockFile);
});
//have server and socket listen to port 8000.
server.listen(8000);
var socket=io.listen(server);
//the lower the number the less chatty the debug becomes.
socket.set('log level', 1);
//initiate the counter.
counter=0;
counterp=0;
connections = {};
function broadcastToConnections(){
for(c in connections){
var con = connections[c];
con.emit('c_display_counter', counter);
}
}
function connectionClose(socket){
delete connections[socket.id];
}
//3. on client connect.
//http://<address>/apps/press_k
socket.on('connection', function(socket){
console.log("Client connected.");
//increment counter on clients command.
socket.on('s_increment_counter',function(data){
counter++;
console.log('New counter:'+counter);
broadcastToConnections();
});
connections[socket.id] = socket;//here we store our connection in connections obj
socket.on('close', connectionClose);//we listen to close event to remove our connection from connection obj
});

Related

Emitting at every second in socket.io don't work

I have a node.js server, on which a socket.io is supposed to emit something every second. But at the client side, the clients get the data more than every 30 seconds. Anybody has any idea why this is happening and what's the solution?
var redis = require("redis"),
client = redis.createClient();
const nsp_page=io.of('/myPage');
nsp_page.on('connection', function(socket){
(function update_index() {
client.get('boxIndex', function (err, data) {
socket.emit('boxIndex',data);
});
setTimeout(update_index,1000);
})();
});
Thanks
But at the client side, the clients get the data more than every 30 seconds. Anybody has any idea why this is happening and what's the solution?
Because your code sends the data every second. This line of code:
setTimeout(update_index,1000);
Causes it to call udpate_index() every second, not every 30 seconds. If you want it to update every 30 seconds, you would change that to this:
setTimeout(update_index, 30 * 1000);
But, note that the way your code works is very inefficient because you're calling the exact same client.get() for every single connected client. That's really inefficient. It would make more sense to call client.get() once and then broadcast that data to every connected client:
const redis = require("redis");
const client = redis.createClient();
const nsp_page = io.of('/myPage');
setInterval(() => {
client.get('boxIndex', function (err, data) {
if (err) {
console.log("boxIndex not found", err);
} else {
// send to all connected clients
nsp_page.emit('boxIndex', data);
}
});
}, 30 * 1000)
When you have lots of connected clients, this will do only one query of your redis database every 30 seconds and use that result for all clients instead of calling redis separately for every single connected client.

Count number of online users with nodeJS and Socket.io without duplicates

I'm having trouble displaying the correct amount of online users. A question similar to this have shown me that I can count the users this way:
var count = 0
socket.on('connection', function(client) {
count++;
client.broadcast({count:count})
client.on('disconnect', function(){
count--;
})
})
The issue I'm constantly running into with that method is that when a user happens to reload the page too quickly, the counter pulls in too much it can throw out.
As you can see, on the right side of the image, a user spammed the reload and it caught more users online than there actually is. (There was only one user on the server at this time)
My question is is there a better or more reliable way to count the exact amount users online without the extra 'virtual users', without using the users++/users-- method?
If they're logging in as a user, then you should authenticate them to the socket. Use that authentication to see if they already have a session, and disconnect them decrementing the count, before you increment it again with the new session.
An example below. The clients objects stores the connected clients, with values being the sockets they're connected to.
var clients = {};
socket.on('connection', function(client) {
//Authenticate the client (Using query string parameters, auth tokens, etc...), and return the userID if the user.
var userId = authenticate(client);
if ( !userId ) {
//Bad authentication, disconnect them
client.disconnect();
return;
}
if (clients[userId]) {
//They already have a session, disconnect
clients[userId].disconnect();
}
//Set session here
clients[userId] = client;
client.broadcast({count: Object.keys(clients).length})
client.on('disconnect', function(){
delete clients[userId];
})
})
Could do this pretty cleanly with the Observable pattern (using RxJS v5 here):
const { Observable } = require('rxjs')
const connections = Observable.fromEvent(socket, 'connection').mapTo(1)
const disconnections = Observable.fromEvent(socket, 'disconnect').mapTo(-1)
// emit 1 for connection, -1 for disconnection
Observable.merge(connections, disconnections)
.scan((total, change) => total + change, 0) // emit total
.subscribe(count => client.broadcast({ count }))
You can use Server Sent Events for this purpose really.
Take a look at https://javascript.info/server-sent-events

socket io js delay disconnect emit

socket.on('disconnect') is fired immediately on client side, but server side it will take one minute.
scenario 1:- if client is disconnect due to internet problem, server side disconnect emits after 1 minutes, any way to find immediately on server side.
i tried to change the pingTimeout= 30000 and pingInterval=12000 but every 30 seconds it reconnecting and connect.
You can use a timeout delay and the socket disconnection event to make it easier to disconnect and reconnect.
When your socket connects, add them to a bucket object.
if (socket.request.sessionID && !bucket[socket.request.sessionID]) {
bucket[socket.request.session.player.id] = socket.id; //nuuu they stealin mah bukkit
}
This adds the player's id (which I track through node session tethered into the socket) as a key to the socketId
//object for delayed log out
let disconnection = {
sid : null, //socket id
delay : null //timeout id
};
Create an object that stores your socket ID so you can track who was disconnected.
socket.on('disconnect', function () {
disconnection.sid = socket.request.sessionID; //grab session id
disconnection.delay = setTimeout(() => {
//set timeout to variable, in case of reconnection
delete bucket[socket.request.sessionID];
//emit the disconnection event
}, 60000);
});
When the user disconnects, set a timeout for however long they have to reconnect.
Upon reconnection, simply do:
io.sockets.on("connection", socket => {
//check for disconnection, compare socket ids, and remove timeout if sockets match
if (disconnection.delay && disconnection.sid == socket.request.sessionID) {
clearTimeout(disconnection.delay);
disconnection.sid = null;
}
});
This will check the reconnection for an existing socketID and clear the timeout, successfully establishing the connection once more.
Note that this code is a copy of SourceUndead (my game) so some of it might not directly translate to you (for instance, my socket variable is bound with the session as well), but the concept of a timeout disconnection is the same.

Socket.io message event firing multiple times

I was trying to learn node and started creating a mashup with socket.io
The message transportation have begin but I have run into some trouble.
The message event is firing multiple times leading to a single message appearing multiple times on the recipient's box. I have routed the socket to exports.chat and was wondering if that is causing the problem?
To narrow down the problem: the messages are firing the number of times = the sequence of connection of the client. That is, if a client connects second, his messages will fire twice. three times for the client connecting third.
Here is the code snippet:
exports.chat = function(io, pseudoArray, req, res){
res.render('chat', {title: 'ChatPanel.'});
var users = 0;
io.sockets.on('connection', function (socket) { // First connection
users += 1;
// reloadUsers(io, users);
socket.on('message', function (data) { // Broadcast the message to all
if(pseudoSet(socket)) {
var transmit = {date : new Date().toISOString(), pseudo : returnPseudo(socket), message : data};
socket.broadcast.emit('message', transmit);
console.log("user "+ transmit['pseudo'] +" said \""+data+"\"");
}
});
socket.set('pseudo', req.session.user, function(){
pseudoArray.push(req.session.user);
socket.emit('pseudoStatus', 'ok');
console.log("user " + req.session.user + " connected");
});
socket.on('disconnect', function () { // Disconnection of the client
users -= 1;
// reloadUsers();
if (pseudoSet(socket)) {
var pseudo;
socket.get('pseudo', function(err, name) {
pseudo = name;
});
var index = pseudoArray.indexOf(pseudo);
pseudo.slice(index - 1, 1);
}
});
});
};
The whole part of socket.io code has to go outside external.chat function. Socket IO has to bind with the http/app server, you should not handle it within each request.
the messages are firing the number of times = the sequence of connection of the client
What essentially happening is, each time a new request arrives you are registering a event handler for message, hence it is fired as many times as the you have accessed chat URL.
io.socket.on('message', function (data) {...})
So I had the same problem. The solution is to close all your listeners on the socket.on('disconnect') event, this is what my code looks like -
socket.on('disconnect', function () {
socket.removeAllListeners('send message');
socket.removeAllListeners('disconnect');
io.removeAllListeners('connection');
});
Might not need to call it on disconnect, not sure but I do it anyway.
I think this misbehavior is because you are attempting to use one of the handful of built-in/reserved event names "message" as an application-specific message. To confirm, change your event name to "message2" or something else and see if the problem goes away. I believe at least "connect", "disconnect", and "message" are reserved. https://github.com/LearnBoost/socket.io/wiki/Exposed-events
Link: https://socket.io/docs/v3/listening-to-events/#socketoffeventname-listener
Please use socket.off method to removes the specified listener from the listener array for the event named eventName.
socket.off("message").on("message", this.UpdateChat);
Restarting the server can be responsible for several identical event listeners on the client side. If the client has not reloaded (restarted) you have to make sure that the old event listeners are deleted on the client side when establishing a new connection. You can do that with
io.socket.removeAllListeners()
SOCKET.IO v3.x
I don't really know why reserve event in socket.io are firing up multiple times, hence I make a logic that fits to our needs and address this problem,
I simply create a global mutable value that could change everytime the 'disconnect' fire up, here's the logic I did
let ACTIVE_USERS = []; //assume the propery of an object is { name, socket_id }
const connections = (socket) => {
socket.on("disconnect", (reason) => {
//lets check how many times it fires up
console.log('YOW', reason);
//mutate the ACTIVE_USERS
ACTIVE_USERS = ACTIVE_USERS .filter(user => {
//lets figure it out if this thing helps us
if(user.socket_id === socket.id){
console.log('HEY!!!');
socket.broadcast.emit('disconnected-user',[
message: `${user.name} has been disconnected`
})
}
})
});
}
and the result of it is here

Socket.IO socket.id changing for same client without reconnect

I'm making an application where I need to map sockets to some object. I thought I would be safe using the socket.id variable provided by Socket.IO. Unfortunately while debugging I found out that the socket.id changed without the client disconnecting/reconnecting or whatever.
Here's a small piece of code to show what I'm trying to do:
var io = require('socket.io').listen(8080),
var myVariable = new Array();
// Main event loop
io.sockets.on('connection', function (socket) {
socket.on('myEvent', function(data) {
myVariable[socket.id] = data;
}
// 'someOtherEvent' which uses the mapped data.
socket.on('someOtherEvent', function(data) {
// Doesn't always work because socket.id has changed and var is empty
doSomethingWith(myVariable[socket.id]);
}
});
I'm not sure but I don't think this is the desired effect. Why does this happen and how would I work around it?
I was using node-inspector to debug my application and because I was holding the code on some breakpoint it disconnected the client (probably some timeout client side). That's why I got a new socket.id when I continued the code execution.
Looks like 'someOtherEvent' emited before 'myEvent' or socket has been reconnected. I use socket.id as connection identifier on two different projects in production and it works without any problems.

Categories