14

I started looking into node and socket.io.

I already have created a simple chat application and I am amazed at how easy it was.

Now, I would like to take a little bit further and provide a list of online users that have the ability to chat with each other in private.

What would be the best way to approach this?

I read on 0.7's new room feature. Would that be a way to go? Dynamically create a new room each time 2 users need to chat in private? But how the second user is going to be notified of the new room created, so that he can connect there?

Is it better to handle all the above logic myself? Store the rooms and users server side and loop through them each time and send messages to the appropriate ones?

Thanks

Thomas
  • 4,641
  • 13
  • 44
  • 67

2 Answers2

18

If the only functionality you want is for two people to be able to send messages to one another (and not groups of people to have a room), then the logic could be something like this:

  1. When a user connects, store their connection in an object keyed by their username (or in any other data structure that ensures you can find a specific user's connection).
  2. When a Bob wants to talk to Jeff, send the server an event stating such.
  3. The server looks up Jeff's Socket.IO connection in the object from step 1.
  4. The server uses this connection to send Jeff (and only Jeff) the private message.
Michelle Tilley
  • 157,729
  • 40
  • 374
  • 311
  • Right. You are both correct. What if I want to have groups of people – Thomas Sep 17 '11 at 05:07
  • Either use the built-in Socket.IO rooms feature, or roll your own by grouping connections together in some sort of data structure, and then iterate over all of the members in a room and send the data in each one. – Michelle Tilley Sep 17 '11 at 05:15
  • I would prefer to use the builtin functionality, but how can you dynamically create and destroy rooms? – Thomas Sep 17 '11 at 06:47
  • 2
    You can read about rooms on the [Readme](https://github.com/learnboost/socket.io), but basically you call `socket.join('name of room')` to add a socket to a room, and call `io.sockets.in('name of room').emit(...)` to emit a message to the sockets in that room. – Michelle Tilley Sep 17 '11 at 07:14
  • Thanks. Is there a method for destroying a room? – Thomas Sep 17 '11 at 17:47
  • Node will automatically clean up the data structure if a room is empty. To get sockets out of a room, call `socket.leave('name of room')`. – Michelle Tilley Sep 17 '11 at 18:09
  • @MichelleTilley Are lookup lists for sockets till used to manage specific client communication? – Nick Pineda Feb 06 '16 at 21:24
  • @MichelleTilley Thank you, could you please help me solve my question too? Perhaps with a basic code example? I am sure you can do this. http://stackoverflow.com/questions/35385609/random-chat-with-2-users-at-a-time-socket-io – Faizan Feb 13 '16 at 21:40
  • @MichelleTilley What if I only want 2 users to randomly connect with each other and start chatting? What if 3rd person comes in ? he should be connected to someone who is not chatting with anyone. – Faizan Feb 13 '16 at 21:45
  • Can you provide an example? I don't get it fully – SwiftiSwift Nov 28 '19 at 00:31
  • I have learned more at this comment than official documentation. – Fernando Torres Jan 26 '20 at 05:57
1

Hej Thomas

if theres only 2 users talking you dont need use publish att all just send that message from the client to the server and let the server locate the other client and send it down.

megakorre
  • 2,213
  • 16
  • 23
  • No. That's not the case. Say I have 100 users and ie Bob wants to talk with Jeff – Thomas Sep 16 '11 at 15:39
  • 2
    right that was my point if bob wants to talk to jeff they dont need there own room just send it directly to jeff and emit "privateMsg" and it will only go to jeff. – megakorre Sep 16 '11 at 18:08