0

I would like to create a live notification center by using Web Socket in Python. I have a feature that send / emit a message to specific user.

I have read about socket id and room. I think there are a couple way to do direct user interaction using both feature from Socket IO.

First:

It is said in the docs that every client / socket connected to the socket server, they were giving an unique socket#id and joined a room identified by id. I have an idea that I do not need create a room again for a user, and just by using this default room.

enter image description here

By using these, I want to create a mapping of socket#id with userid (logged user id), and store it into redis. Like, if I want to send a message to a user id, I just need to search for it's socket#id.

The question for this first way is, is the socket#id will be the same if a client disconnect and reconnect soon the connection can be established ?

Second:

I have read many sources about using room for direct message. For connected client or socket, I just need to create a new room with id of the user id that sent from client (after user login).

The advantages of this way is, if the user is using same user id for several device, I can group them in a room. So, if I want to notify to all device, I just need to send the message for room of id user.

The question of this way, is the user will be moved from default room from first way or they will be on 2 room (1 default, 1 specified room by user id) ?

Thank You

Adityo Setyonugroho
  • 877
  • 1
  • 11
  • 29

2 Answers2

1

This is the way i do it

a users socketid will be different on every page request, and if a user have 3 tabs open he will have a different socketid on every tab, so if you want a user to get the notification in every tab u need to send it to 3 socketids

a solution for this is to make an object of connected users, you can add the userid/username as a key in your object and make an array of all socketids for this user, make sure to remove the socketid when the user disconnect too, and when u want to send a notification u just loop over the array and send to all of the users socketids

Asaf Aviv
  • 11,279
  • 1
  • 28
  • 45
1

The question for this first way is, is the socket#id will be the same if a client disconnect and reconnect soon the connection can be established ?

No. Each connection gets a randomly assigned socket id.

The question of this way, is the user will be moved from default room from first way or they will be on 2 room (1 default, 1 specified room by user id) ?

Users can be in multiple rooms at a time. If you create a room with your application generated user ids, and then put all the socket connections from the user into that room, each of these clients will be in two rooms, the one that matches the socket id and the one that matches your user id. You can then emit a message to each socket individually or to all sockets that belong to the user.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152