Reputation: 349
I am building a real-time web-application with multiple clients and a node.js server to handle state and broadcast event changes from clients. I have implemented Socket.io as transport mechanism between clients and server. When a client performs a specific action an event this is send to the server, which is broadcasted to other clients on the network.
However, sometimes when an array of events is recieved by the server, the server doesn't broadcast it but instead sends it back to the sending client - or sends it to all connected sockets including the sending client.
Here is a snippet of the server implementation, which basically listens for client events, collects them in an array which is broadcasted every 2 seconds.
If you want, I can include relevant snippets from the client implementation.
// Require HTTP module (to start server) and Socket.IO
var http = require('http'), io = require('socket.io'), users = require('./users');
// Commands to client
var USER_LOGIN = 0,
USER_LIST = 1,
USER_CONNECTED = 2,
USER_DISCONNECTED = 3,
COMMAND = 4,
COMMAND_SETNICKNAME = 0,
COMMAND_POSITION = 1,
COMMAND_MOUSEDOWN = 2,
var array = [];
var activeUsers = {};
// Start the server at port 8080
var server = http.createServer(function(req, res){
res.writeHead(200,{ 'Content-Type': 'text/html' });
});
server.listen(8080);
// Create a Socket.IO instance, passing it our server
var io = io.listen(server);
// Add a connect listener
io.sockets.on('connection', function(client){
console.log("started");
// Create periodical which ends a JSON message to the client every 2 seconds
var interval = setInterval(function() {
if(array.length >0) {
console.log("Sending to clients " + array);
//client.send(JSON.stringify(array));
client.broadcast.send(JSON.stringify(array));
array = [];
}
},2000);
// Success! Now listen to messages to be received
client.on('message',function(event){
console.log("--Reading event " + event);
var eventArray = JSON.parse(event);
for(var i = 0; i < eventArray.length; i++) {
var dataArray = eventArray[i];
var userId = parseInt(dataArray[0]);
var position = 1;
console.log("current element in array " + dataArray);
switch (parseInt(dataArray[position++])) {
case USER_LOGIN:
// Log in user with USERID.
var username = dataArray[position++];
console.log("Attempting to log in with user " + username);
// If valid, send USER_LOGIN broadcast to clients
if(login(username)) {
array.push([0, USER_LOGIN, login(username)]);
}
break;
Upvotes: 0
Views: 790
Reputation: 21366
io.sockets.on('connection', function(client){
console.log("started");
// Create periodical which ends a JSON message to the client every 2 seconds
var interval = setInterval(function() {
if(array.length >0) {
console.log("Sending to clients " + array);
//client.send(JSON.stringify(array));
client.broadcast.send(JSON.stringify(array));
array = [];
}
},2000);
you are creating one scheduler for each connection which is going to be a mess. Because you are creating the interval inside on('connection',
And I don't see any point of calling clients in an interval because you have a method to push the changes on some clients change event. That is the advantage of socket.io . Instead of using an interval you can just broadcast the change in another listener from client change event like this.
client.on('onSomechangeInClient',function(event){
//update array here..
client.broadcast.send(JSON.stringify(array));
});
Upvotes: 1