0

I've been trying for some days to develop a real-time notification web app using node.js and the socket.io module.

WHAT I HAVE DONE

I have two parts.1) Admins and 2) Users.

So I have initialized two different namespaces on the client side. Fo example: var socket = io(); for Admins and var socket = io('/Users'); for Users.The time a User sends a message, message goes to ALL of the Admins (1st step made). I am using this command to emit: nsp3.emit('chat message', obj);.nsp3 is var nsp3 = io.of('/Admins'); on the server and obj is a json structure.

WHAT I WANT TO ACCOMPLISH

When a message is sended by some User then the server receives the request, makes a SELECT query to db table based on some criteria and emit the message to a part of Admins who meet these criteria.

Which is the best way to do this?

GeoDim
  • 131
  • 2
  • 3
  • 10
  • When an admin connects, push their socket and details about them to an array, then you can search for them later on and do `admins[0].socket.emit(ect)` which should just push a message to that socket like in [this question](https://stackoverflow.com/questions/4647348/send-message-to-specific-client-with-socket-io-and-node-js) – George Sep 14 '17 at 08:50
  • Yes, but I noticed that on the page reload `socket.id` changes..I think this is a problem – GeoDim Sep 14 '17 at 08:57
  • `socket.id` will change as they would have dropped their old connection and made a new one – George Sep 14 '17 at 08:58
  • So if I stored `admin details` on each connection to an array, I will be able to send a message to a part of them after a `SELECT` query and if (for example) `table username == admins array username` emmit the message?Am I right? – GeoDim Sep 14 '17 at 09:04
  • Yeah, you should be able to – George Sep 14 '17 at 09:04
  • Sorry but even if admin's page reloads I will still be able to send a message to him?And what if admin opens 2 browsers or more at the same time? – GeoDim Sep 14 '17 at 09:07
  • if the admin page is reloaded you should remove them from the array when they disconnect, and add them when they connect again. With two browsers open, you'll need to push to both sockets (if it makes two socket connections and doesn't just use one) – George Sep 14 '17 at 09:08
  • I'll make a try.If you can give me some example or tutorial about this please post it.Thanks. – GeoDim Sep 14 '17 at 09:46

0 Answers0