Socket.io get socket by id not working - node.js

I am trying to access a socket by its id. I have seen this and this stackoverflow posts. However,
io.sockets.connected[socket.id]
returns undefined.
This also doesn't work :
io.to(socket.id).emit("myMessage");
Socket is connected to a namespace (socket.id returns /playNS#1HhBtUM-6O_YsRwmAAAF) and socket.io version is 1.4.5. What am I doing wrong?

If a socket is connected to a namespace, the first part of socket.id contains that namespace (e.g. /playNS#1HhBtUM-6O_YsRwmAAAF) , but io.sockets.connected property for that socket would be /#1HhBtUM-6O_YsRwmAAAF
In order to properly retrieve the socket I used io.of('/namespace').connected[socket.id].
Similarly, io.of("/namespace").to(socket.id).emit("myMessage"); to send a message

Related

How to get handle event name on socket-io nodeJs

I am working on socket application. and Client emit some event and on the server I handle that event. I want to extract that emitted event from socket object or server. I parsed are socket object but there is nothing which I am looking for. FOR EXAMPLE:
user emit socket.emit('/user/list') on server and handle socket.on('/user/list').. I want to get '/user/list' from socket object. I parsed object but I can't found. If someone know how to get that. I will be appreciated. Thanks in Advance

Is socket id necessary while emitting?

If am emitting from backend and socket id is undefined then will client side get the event that i emitted?
I have also seen that even if the socket id is undefined, client side still gets events but i am not sure how this works.
I am storing socket ids in redis but sometimes i get undefined.
Socket Ids change everytime.
You could decide to save a particular room name in the backend, and use it instead of socket id.
Then fetch it on the frontend and send it to the backend with an emit.
Receive on the server with socket.on , and save it to use it, for example with socket.roomname = roomname

get contents of the request object in socket.io

I am new to socket.io and I try to tell apart every client that connects to my node server.
I want to send data to just one client, not broadcast the same data to all of them. So, I have to find a unique piece of each client.
If I got it, I have to do something like
io.on('connection', function (socket) {
var origin = socket.request;
console.log("hey, so , new ws connection is > "+origin);
});
but I get [object, Object].
What are the contents of this object, so I can get something unique like an id. Also what other data this object contains that may come handy in the future?
Thanks
UPDATE
I saw the socket.id. But, everytime I refresh, the id changes. I want something to be stable, otherwise if the client refreshes for some reason, will no longer be related with the first id. Any tips?
every connect have socket.id is unique, if you use socket-io adapter you can use socket handshake info. on Browser for geting socket.id try
$(function () {
setTimeout(function () {
$("#socketId").val(socket.id);
},100);
});
if console.log you will see socket.id in future
You'd use the socket.id property to get hold of the unique identifier for the socket (client socket).
As for other data this object contains, there are plenty that are useful, but the usefulness is dependant on the application. Have a look at the following link for available properties/methods on the socket object. http://socket.io/docs/server-api/
Some very useful methods are emit, join, leave, to, in and properties include id and rooms.

Solving Socket.IO socket undefined on connection from Arduino?

I'm trying to connect to socket.io from arduino. The service works from the browser, but if I try to connect from the arduino, when I look in the node log I created via console.log when a connection happens, the socket is undefined, but picks up the connection. Below is the protocol switching request, which returns a 404 not found, but socket.io does register a connection but doesn't define the socket.
client.print(F("GET /socket.io/1/websocket/"));
client.print(sid);
client.println(F(" HTTP/1.1"));
client.print(F("Host: "));
client.print(hostname);
client.fastrprint(F(":"));
char portBuffer[10];
itoa(port, portBuffer, 10);
client.fastrprint(portBuffer);
client.print(F("Sec-WebSocket-Key: ")); //dGhlIHNhbXBsZSBub25jZQ==
client.print(F("dGhlIHNhbXBsZSBub25jZQ=="));
client.println(F("Origin: ArduinoSocketIOClient"));
client.println(F("Sec-WebSocket-Protocol: chat, superchat"));
client.println(F("Sec-WebSocket-Version: 13"));
client.println(F("Upgrade: websocket"));
client.println(F("Connection: Upgrade\r\n"));
I was thinking maybe I formed the request incorrectly, but I can't find any information for the socket.io protocol switching request so I don't know what to change. What I have done is loosely based on what I read in web socket protocol stand.

One socket joins several namespaces

I have an application that uses several namespaces to differentiate between different kind of clients, so since the beginning I separate them in this manner (I'm using cluster and spawn 4 processes)
//server code
io.of("/TYPE_ONE").on("connection", function(socket){
console.log("Client connected to TYPE_ONE with id:\t"+socket.id+"\t"+process.env.NODE_WORKER_ID);
});
io.of("/TYPE_TWO").on("connection", function(socket){
console.log("Client connected to TYPE_TWO with id:\t"+socket.id+"\t"+process.env.NODE_WORKER_ID);
});
//client code
//for type one
socket = io.connect("http://mydomain.com/TYPE_ONE", socketOptions);
//different files always, only one type sent to each client
//for type two
socket = io.connect("http://mydomain.com/TYPE_TWO", socketOptions);
All of a sudden, after looking at the console, when a single client connects and I get the following output:
Client connected to TYPE_ONE with id: 1234 3
.
.
.
Client connected to TYPE_TWO with it: 1234 3
(same id and workerId as previous connection)
I'm certain that there is only one connection being made to the server, t
I'm wondering what could be causing this? Because Ive looked through my code, and simplified the methods to the stubs I just showed, and can't seem to find the issue.
Thanks for your help.
There are no errors in your code: you have only one connection, therefore socket connects to namespaces with one socket.id. I don't know how to make many connections, maybe you should try two ports of connection, or connect/listen server multiple times.
To solve your problem i would use one connection, but think about how to sturcure your site. For example, use rooms and store in session NEW user id's to figure out in what room or namespace to put user.
"Multiple namespaces and multiple rooms can share the same (WebSocket) connection"
socket.io rooms or namespacing?
If you want to differentiate clients, you can put theme in another rooms in one namespace and having their socket.id check in what room they are (or join rooms with socket.id name (.joind(socket.id)).
https://github.com/LearnBoost/socket.io/wiki/Rooms
It turns out, at some point in my code, I had a io.of("/TYPE_ONE").socket(socket.id).emit(message); and that socket belonged to the namespace TYPE_TWO, so it seems whenever you send a message to a socket from a namespace to which he isn't connected, this will automatically connect it to that said namespace. Weird though.

Resources