nodejs socket emit return undefined - javascript

I am trying to send to clients whenever a new connection issued.
My server side javascripts is :
var clients = 0;
io.sockets.on('connection', function (socket) {
clients ++;
console.log(clients);
socket.broadcast.emit('connect',socket.clients);
}
and my client side js to handle:
socket.on('connect', function (data) {
console.log('on'+data);});
the output is undefined (on undefined)
Where 's things wrong?

socket.broadcast.emit('connect',socket.clients);
^^^^^^^^^^^^^^ this should be 'clients'
Also, connect is a pre-defined event with socket.io, so you shouldn't reuse it or it might give unexpected results.
FWIW: socket.broadcast.emit will send a message to all connections except the current one. If that's not what you want (you want the message to be sent to all connections), use io.sockets.emit instead).

Related

get tab's socket instance on server

I have to get socket instance in my ajax request on server in node.js module. Here is my code.
app.js
io.set('authorization', function (handshake, callback) {
if (handshake.headers.cookie) {
cookieParser(handshake, null, function(err) {
handshake.sessionID = handshake.signedCookies['express.sid'];
});
} else { return callback('No cookie transmitted.', false); }
});
io.sockets.on('connection', function(socket) {
var session = socket.handshake.session;
var userid = session.userid;
socket.join("room");
//make user offline
socket.on('disconnect', function() {
//my code goes here...
//make user offline
})
});
Now in one of my ajax request, I want socket instance
app.post('/logout', function (req, res) {
//here i want socket instance, so I can emit message to all socket, accept this.
});
As I know, each tab creates it's own new socket connection, but session is unique between all tabs of browser. So, How Do I store socket for each tab on server side, where I can find easily socket instance, and then broadcast message to all sockets, excluding that socket which is creating events. (means user's active tab's socket connection)
any guess.
thanks
In my app, I can do what you say because i use namespace and room and so in a room i can find every socket of someone.
io.of('/user').clients(idRoom);
So that i can remove every socket of the user. But if you cannot use this, i think in your app you will have to implement outside socket.io a class for someone (using session as a way to see if it's already have a socket open or if you have to create a new instance). And in this class, have a socket table so that you will be able to handle socket of someone.
In my case, i do the same except that i use the room of socket.io to do that.
And to broadcast to every socket, it depends what is your app. If your app send to anyone in the same namespace, it doesn't change anything because the socket of the same session will also receive the message. But if not, you will have to implement a function to emit to every socket of the table i suggested above.
In my case i use the 'exclude' to ensure the current socket doesn't receive the message but usually you can use broadcast.
io.of('/user').in(this.id).except(socket.id).emit('msg', { text: text,type:person});
To conclude, socket.io will not help you to handle session and several socket for one user/session but you can manage to deal with it using room feature (in my case it was the best way), or implement a user class where you will manage a table of your session sockets.

nodejs send list of all clients fail

js.
Currently I have done several tests successfully
Hello world
server-client communication with events
many rooms
But i can't send the client, to the clients, i use socket.io
The reason I want to do this, is to have a list of current clients
EDIT:
I need to store information for each user in server side
i try use
var io = require('socket.io');
var socket = io.listen(9876);
socket.on('connection', function(client) {
client.join('room');
console.log('new client ' + client.toString());
client.in('room').emit('list', client ); //<-- Error here
client.on('message', function(event) {
console.log('client message! ', event);
client.send(event);
});
client.on('chat', function(data) {
client.broadcast.in('room').emit('LDS', data);
client.in('room').emit('LDS', data);
});
client.on('disconnect', function() {
console.log('out');
});
});
but throws this error
connections property is deprecated. Use getConnections() method
C:\Repos\InWeb\NO\node_modules\socket.io\lib\parser.js:75
data = JSON.stringify(ev);
^
TypeError: Converting circular structure to JSON
at Object.stringify (native)
The original idea is send a list of clients, like this
var listClients;
socket.on('connection', function(client) {
client.join('room');
listClients.push(client);
client.in('room').emit('list', listClients); //<-- But throws same error
I guess that the error is given by the client context
I can fix it?
You send whole object client, you should use something like this
Socket.io Connected User Count
or create array with name and id and send array not whole node object.

socket.io not working on several clients

I'm trying to build a simple chat client and am having some issues getting it working across multiple clients. Chances are I've missed something really simple. When I send something from one client it is logged in that client but not in any others.
Server:
var io = require('socket.io').listen(5000);
io.sockets.on('connection', function (socket) {
socket.on('send', function (data) {
socket.emit('receive', data);
});
});
Client:
var socket = io.connect('http://localhost:5000');
socket.on('receive', function(data){
console.log("Data received at " + new Date() + ": " + data);
});
The socket variable that gets passed to your callback function is a handle on the client that connected, so socket.emit is behaving correctly, i.e. it should only send to the client that originated it.
Try:
socket.broadcast.emit('receive', data);
to send to everybody except the originating client, or
io.sockets.emit('receive', data);
to send to all clients including the originator.
You want to emit on all sockets, not just the one that sent the message.
So you should be using:
io.sockets.emit('recieve', data);
This is assuming that you aren't logging the data on the sending client before sending it to the server. In that case you'll want to use:
socket.broadcast.emit('recieve', data);
Which will send the message to all connected clients except the sender.
See here for reference on Socket.io
Edit: Trevor beat me to it. However, for some additional clarification: io.sockets is the same as io.of(''). Which is handy to know for when you start using namespaces in a scoket.io app.

Socket.io dilemma with clients.length (where clients === io.sockets.clients();)

Normally, when I send some JSON from one client to another, it works fine. But if there is only one client, it still sends packets. My solution was to (on the server-side, in node.js):
var clients = io.sockets.clients();
if(clients.length > 1){
// send stuff to other client
}
But when I do that, something extraordinarily strange occurs. I'll open up a client, start instigating the actions during the if statement, and the console, which prints the JSON before sending it, does nothing, because there is only one client. When I open up another client and instigate the actions during the if statement, the console will print out the JSON and send it to my other client. However, if I go over to my first client that I opened and instigate the actions in the if statement, the console won't print anything out, and it won't send anything. Why? I tried changing the if statement to say:
if("hello" === "hello"){
// do the same stuff as before
}
and both clients could send JSON to one another, but because I had changed the if statement, with one client open I would still send useless packets. For obvious reasons, the optimal situation would be if it wouldn't send packets with one client, but would send packets properly with more than one.
I hope that was clear, I'm sorry if that wasn't.
If I need to, I'll post more code.
EDIT:
Here is the code that updates the array:
var clients = io.sockets.clients();
io.sockets.on("connection", function (socket) {
clients.length++;
socket.on("disconnect", function() {
clients.length--;
console.log(clients.length + " clients are connected");
});
console.log(clients.length + " clients are connected");
});
Haven't done any note.js lately, maybe this is bogus. But I thought: maybe the clients variable is defined for each client when he connects, and not updated later on. This seems the logic thing to me. I think that what you need to do is: fire an event to all clients when a new client connects, and update the "clients" variable.
I'm not sure how your code works (what's this: clients.length++ ?? Incrementation of array's length??) but try doing this the "normal" way:
var clients;
io.sockets.on("connection", function (socket) {
clients = io.sockets.clients();
socket.on("disconnect", function() {
clients = io.sockets.clients();
console.log(clients.length + " clients are connected");
});
console.log(clients.length + " clients are connected");
});
or even better (without holding global reference to connected users):
function get_clients() {
var clients = io.sockets.clients();
console.log(clients.length + " clients are connected");
}
io.sockets.on("connection", function (socket) {
socket.on("disconnect", function() {
get_clients();
});
get_clients();
});
Because io.sockets.clients() returns the array of all connected clients at the moment of calling it (perhaps that's why you get this strange behaviour).

Socket.io with Cluster: iterating over all open connections

I'm running Socket.io multi-threaded with the native cluster functionality provided by Node.js v0.6.0 and later (with RedisStore).
For every new change in state, the server iterates over each connection and sends a message if appropriate. Note: this isn't "broadcasting" to all connections, it's comparing server data with data the client sent on connection to decide whether to send the server data to that particular client. Consider this code sample:
io.sockets.clients().forEach(function (socket) {
socket.get('subscription', function (err, message) {
if(message.someProperty === someServerData) {
socket.emit('position', someServerData);
}
});
This worked fine when there was only one process, but now, the client receives a message for each Node process (ie. if there are 8 Node process running, all clients receive the messages 8 times).
I understand why the issue arises, but I'm not sure of a fix. How can I assign a 1-to-1 relation from one process to only on client. Perhaps something using NODE_WORKER_ID of Cluster?
This previous SO question seems somewhat related, although I'm not sure it's helpful.
This seems like a pretty common request. Surely, I must be missing something?
So if I get this straight you need to emit custom events from the server. You can do that by creating your own custom EventEmitter and triggering events on that emitter, for example:
var io = require('socket.io').listen(80);
events = require('events'),
customEventEmitter = new events.EventEmitter();
io.sockets.on('connection', function (socket) {
// here you handle what happens on the 'positionUpdate' event
// which will be triggered by the server later on
eventEmitter.on('positionUpdate', function (data) {
// here you have a function that checks if a condition between
// the socket connected and your data set as a param is met
if (condition(data,socket)) {
// send a message to each connected socket
// if the condition is met
socket.emit('the new position is...');
}
});
});
// sometime in the future the server will emit one or more positionUpdate events
customEventEmitter.emit('positionUpdate', data);
Another solution would be to have those users join the 'AWE150', so only they will receive updates for 'AWE150', like so:
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
if (client_is_interested_in_AWE) { socket.join('AWE150'); }
io.sockets.in('AWE150').emit('new position here');
});
Resources:
http://spiritconsulting.com.ar/fedex/2010/11/events-with-jquery-nodejs-and-socket-io/

Categories

Resources