Socket.io broadcast stops working if user leaves namespace then rejoins - javascript

My situation is this:
User A creates namespace with name of his choice
User B joins created namespace
User A sends message which is broadcasted by the server and received by User B
User B sends message which is broadcasted by the server and received by User A
User B leaves namespace through client-side socket.disconnect();
User B rejoins namespace
User B sends message which is broadcasted by the server and IS received by User A
User A sends message which is broadcasted by the server and NOT received by User B
Important Notes:
If User B refreshes the page and rejoins that namespace while User A remains there, he can
then send and receive messages normally again.
When User B is in the 'not working' state, he still seems to be getting events through socket emit. In other words, only User A receives events initiated by both parties, where User B only receives events initiated by himself.
Why, when User B leaves and rejoins does the server become a one way street relative to him?
Server Code:
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
server.listen(8080);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
var connection_function = function(socket) {
socket.on('join_chat', function(data) {
socket.set('fb_id', data.id);
socket.emit('joined_chat', data);
socket.broadcast.emit('joined_chat', data);
socket.namespace.connected_users[data.id] = data;
});
socket.on('send_message', function(data) {
data.user = socket.namespace.connected_users[socket.store.data.fb_id];
socket.emit('recieve_msg', data);
socket.broadcast.emit('recieve_msg', data);
});
socket.on('disconnect', function () {
socket.broadcast.emit('left_chat', socket.namespace.connected_users[socket.store.data.fb_id]);
delete socket.namespace.connected_users[socket.store.data.fb_id];
});
}
var chats = {};
var main = io.of('/main');
main.on('connection', function(socket) {
socket.on('new_chat', function(data, fn) {
if(!chats.hasOwnProperty(data.chat_name)) {
chats[data.chat_name] = io.of('/' + data.chat_name);
chats[data.chat_name].on('connection', connection_function);
chats[data.chat_name].connected_users = {};
}
fn(data);
});
});
Update (Friday October 12, 2012)
After some more research it appears that the issue may be with socket.io:
https://github.com/LearnBoost/socket.io-client/issues/251
https://github.com/LearnBoost/socket.io-client/issues/473

Are you sure you don't want to be using socket.io rooms instead?
socket.join(roomName);
io.socket.in(roomName).emit(.....;
I have a working example of rooms here: https://github.com/rcpeters/openMap_me/blob/master/app.js

Related

Express-generator, Socket.io Event issuing multiple times

I have create a node app using express generator. I have integrated socket.io in the application. Since express generator has their own way of creating express server i have followed this procedure to successfully integrate the Socket connection with listening server and made the io available throughout the application via res.io instance.
FILE: bin/www
#!/usr/bin/env node
var app = require('../app').app;
var debug = require('debug')('www:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = require('../app').server;
/app.js
//Express handler
var app = express();
// Socket configuration
var server = require('http').Server(app);
var io = require('socket.io')(server);
app.use(function(req, res, next){
res.io = io;
next();
});
...
module.exports = {app: app, server: server};
But the problem is when i m emitting an event as shown below. My client is reading the data multiple times.
routes/index.js
var clients = 0;
var nsp = res.io.of('/default-namespace');
nsp.on('connection', function (socket) {
clients++;
console.log(clients + ' clients connected!');
socket.on('disconnect', (reason) => {
clients--;
console.log(clients + ' clients connected!');
});
nsp.emit("socketToMe", "New User connected. Current clients:"+ clients);
});
My listener has the following code:
home.pug
var socket = io('/default-namespace');
socket.on('socketToMe', function (data) {
$('#data-div').append($('<li>').text(data));
});
Whenever i refresh the browser in another instance like incoginito my main browser is showing multiple events for the data. Like this
New User connected. Current clients:1
New User connected. Current clients:2
New User connected. Current clients:1
New User connected. Current clients:2
New User connected. Current clients:1
New User connected. Current clients:1
Not sure what is wrong. Can anyone help me on this?
Nodejs is event driven.The res object is not a global variable.
Express middleware runs for every request.
var clients = 0;
var nsp = res.io.of('/default-namespace');
nsp.on('connection', function (socket) {
clients++;
console.log(clients + ' clients connected!');
socket.on('disconnect', (reason) => {
clients--;
console.log(clients + ' clients connected!');
});
nsp.emit("socketToMe", "New User connected. Current
clients:"+clients);
});
Let me explain what happens above.A user requests and req handler is fired and you access the res object and you listen for events.
So for each request, you are listening for socket 'connection' event.That means you are setting multiple event listeners with the same name.Every time you make a request you set a new listener.
You are supposed to set only a single 'connection' listener.
This explains emitting the same event multiple times.
app.use(function(req, res, next){
res.io = io;
next();
});
Instead of using the above middleware function,listen directly on io instance

Express - socket.io - session. Refer to user id as socket id

I am using express-socket.io-session. I think I was able to setup the basic config by seeing the tutorials:
//BASIC CONFIG?
var clients = [];
var session = require("express-session")({
secret: 'some key',
resave: true,
saveUninitialized: true
});
var sharedsession = require("express-socket.io-session");
app.use(session);
io.use(function(socket, next){
next();
});
io.use(sharedsession(session, {
autoSave:true
}));
io.on('connection', function(socket) {
console.log("CLIENT CONNECTED");
var session = socket.handshake.session;
clients.push(socket);
socket.on('disconnect', function() {
console.log("CLIENT DISCONNECTED");
});
});
What I want to be able to do now is to refer to a specific client socket not by the socket but by the session id that should be assigned to that socket. When a user logins this happens:
req.session.user_id = user_id;
//(user_id is equal to DB {0,1,2,3...} ids
I was able to send sockets to specific clients when I did this:
clients[0].emit("to_do",info); // I don't know who is client index 0 compared to the login reference...
I would like to be able to do this or similar:
user_id = 3; // which would have a socket assigned
clients(user_id).emit("to_do",info);
That would mean each client would have a socket assigned to its previously assigned id. How could I do this so I could specify the socket by that id? I am not experienced at all with all of this so sorry for any big mistakes. Thanks
Your problem can be solved by each socket joining a group named after it's id:
socket.join(socket.id);
io.sockets.in(socket.id).emit('to_do', info);
//or
io.sockets.in(clients[0].id).emit('to_do', info);
Well I solved this out by iterating through the clients list and seeing which one had the socket I wanted
I ran into a similar issue, when using express-socket.io-session the user ID in socket.handshake.session.passport changes when a new user login, I used the below to solve it.
var userID;
if (!userID){
userID = socket.handshake.session.userID = socket.handshake.session.passport['user'];
}

Send messages from server to client socket.io

I am trying to send a message from NodeJS server to client using socket.io
However, I found the same practice all over the internet, which is wrapping the emit with io.on('connection', handler) and then making the server listen on a special "channel" event like so:
var io = require('socket.io')();
var socketioJwt = require('socketio-jwt');
var jwtSecret = require('./settings').jwtSecret;
var User = require('./models/users').User;
io.set('authorization', socketioJwt.authorize({
secret: jwtSecret,
handshake: true
}));
var sockets = [];
io.on('connection', function(socket) {
sockets.push(socket);
});
sendLiveUpdates = function(gameSession) {
console.log(sockets);
}
exports.sendLiveUpdates = sendLiveUpdates;
exports.io = io;
My problem is: I want to emit messages outside this on connection wrapper, example from my routes or other scripts. Is it possible?
Thanks.
Yes. You just need to keep a reference to the socket.
// Just an array for sockets... use whatever method you want to reference them
var sockets = [];
io.on('connection', function(socket) {
socket.on('event', function() {
io.emit('another_event', message);
});
// Add the new socket to the array, for messing with later
sockets.push(socket);
});
Then somewhere else in your code...
sockets[0].emit('someEvent');
What I usually do is assign new clients a UUID and add them to an object keyed by this UUID. This comes in handy for logging and what not as well, so I keep a consistent ID everywhere.

socket.io-client connecting, but not emitting

I am making a little encrypted chat app, in the terminal, using socket.io-client and socket.io. The client is able to connect to the server, but is not emitting the username, when its entered.
Client:
var socket = require('socket.io-client')('http://127.0.0.1:3000');
socket.on('connect_error', function(){
console.log('Failed to establish a connection to the servers, or lost connection');
return process.exit();
});
var prompt = require("prompt-sync")()
var news = "Add news: Will be from database. "
var username = prompt("Username>: ")
console.log("Hold on a sec, just checking that!")
console.log("")
if (typeof username === "defined"){
socket.emit('user-name', {usr: 'username'})
}
socket.on('user-name-good',function(socket){
console.log("Okay! Your username looks good, we just require your password")
console.log("If you chose to have no password, please press enter with out pressing space!")
var password = prompt("Password>: ")
if (typeof password !== "defined"){
console.log("Please provide a password!")
return password = prompt("Username>: ")
}
socket.on('user-name-fail',function(socket){
console.log("Sorry, we could not find, "+username+""+"Please register on the website, or, if you have registered ")
return process.exit()
})
}
)
Server code, is based on code from socket.io chat example:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
I have added a error event, this closes the client if a connection to the server fails, so I know its connecting, any help appreciated, I have done research on this topic, and tried a lot of other methods, but to no avail.
Also the connection is made after you submit data, not when the client code is started, what could be causing this?
If you want to send events between client and server you have to:
Send event A from client to the server and server has to be listening for the A event.
If you want to send event B from server to client then client has to be listening for the event B.
Apart from everything else in your code I don't see where you are listening for the 'chat message' event on the client side.
Socket.io is based on these so called 'events'. The code below is going to send 'my_event' event to the server and the trasmitted data is going to be the object { a: 1 }.
socket.emit('my_event', { a: 1 });
If I want to handle this event on the server I have to listen for it:
socket.on('my_event', function(data) {
// data is the object { a: 1 }
// do stuff..
});

How can I have faye-websockets code running in the browser?

I'm new with node.js/express and all and I want to be able to notify any clients in browser about a new message received from some algorithm in the back-end. The publisher algorithm connect to the websocket and writes the message.
As far as I've looked there were examples which recommended websockets but I haven't been able to run that code in browser only in console.
Example client code:
var WebSocket = require('faye-websocket');
var ws = new WebSocket.Client('ws://localhost:1234');
var http = require('http');
var port = process.env.PORT || 1235;
var server = http.createServer()
.listen(port);
// receive a message from the server
ws.on('message', function(event) {
alert(JSON.parse(event.data));
});
Thank you
Found the answer after some trial/error iterations.
The algorithm now does a POST to an URL which in turn triggers a write to sockets for all connected clients via socket.io.
Client code:
var socket = io('http://localhost:7777');
socket.on('message', function (msg) {
document.body.insertAdjacentHTML( 'beforeend', '<div id="myID">'+msg+'</div>' );
});
And on the server, when client connects I retain it's socket into an array so I can write to each one:
Server code:
io.on('connection', function(socket){
console.log('a user connected: '+socket.id);
var id = clientCount++;
clientSockets[id] = socket;
socket.on('disconnect', function(){
console.log('user disconnected');
delete clientSockets[id];
socket = null
});
});
app.post('/alerts', function(req, res) {
req.accepts(['json', 'application']);
console.log("Algo did a POST on /alerts!");
// send the message to all clients
//console.log(req.body);
for(var i in clientSockets) {
clientSockets[i].send(JSON.stringify(req.body));
}
res.send(200);
});
In conclusion, I'm not using faye-websockets but instead socket.io

Categories

Resources