M
M
Mr Morfei2015-04-09 21:33:03
Node.js
Mr Morfei, 2015-04-09 21:33:03

How to change client.id in node.js(socket.io)?

code example:

//тут какой то код...
io.sockets.on("connection", function (client)
{
//...
client.on("message", function (r)
         //...
  {  io.sockets.to(client.id).emit("message", b))
//тут какой то код...

Question: how to change (if possible) client.id to real mysql.ID so that emit works later?
PS But I feel in one place that I'm digging wrong, the essence of the task is this: I open 2, 3 tabs in the browser with the same user authorized and connected to socket.io, correspondence between users works fine, but when I write a message, it only comes in one tab, the rest, as I understand it, are disconnected (tabs), although the user is online. How to combine all tabs and browsers, any IP, under one user, so that the message sent to him comes to all "holes" that are open from his mysql id ?
In general, kind people help me, I don’t know where to dig :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Kustikov, 2015-04-16
@art1z

client.id is a unique session ID for each server, and in theory, if the disconnect is handled incorrectly, it can be assigned to a completely different client. In general, controlling the direct relationship between socket.io client.id and the client ID in the database is not an easy task.
It's much easier to store this ID on the client (localStorage) and pass it with every message and maintain one up-to-date mapping of MySQL ID <=> client.id in server memory.
There is a well-known problem with parallel open tabs - by default browsers can keep 2-3 permanently open connections (websocket) per URL. Solved by synchronizing tabs via localStorage: html5demos.com/storage-events
Or https://groups.google.com/forum/#!topic/socket_io/... ,
orstackoverflow.com/questions/18654567/save-socket-i...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question