0

I have a node.js/socket.io webapp that is currently working correctly polling an API and populating the html page with the emitted results.

My problem is that multiple people need to use this and I would like to separate their instances so that each person will only receive the results of their query.

Right now, when anyone uses the site it will return results of another user that may be also using the site. I have tried to get around this using this method:

var clients = {};
 io.sockets.on('connection', function(socket){
    console.log("this is "+socket.id);
    clients.id = socket.id;
 })

io.sockets.socket(clients.id).emit('progress',{info:listing});

Of course this gets replaced with each new user that logs into the site so then everything that was emitted to the original user is now being emitted to the new user.

What I want to know is if there is any built-in function to get around this or if I should proceed with another persistent store.

Any help would be greatly appreciated.

Edit

By storing the socket object in the express.sessionStore instead of just in the program. io.sockets.on(function(socket){ request.sessionStore.socket = socket; })

The above code now works and only emits to the event originator.

MonsterWimp757
  • 1,197
  • 2
  • 14
  • 29

1 Answers1

0

This looks like it's been answered in another thread. The idea is to create an array of clients, and associated it to some type of client/user identification, like an ID or name.

Sending a message to a client via its socket.id

Community
  • 1
  • 1
Jack
  • 419
  • 2
  • 5