What is the best method to send user specific data from node server to client using socket io. Should I create each socket connections for users (probably hit the performance) or should I open each channel based on user.
----- Update on 09/16/2014-----
Recently i have tried creating multiple rooms corresponding to each userid based on login and in the UI layer i'm only getting user specific data. Everything works fine.
But if we look into Chrome Web Developer Websocket tab and click the connection and Frames subtab i could still see other user data. Does this means we have to spin off different socket connection for each user rather than creating rooms for user.
I have broadcasted data for 3 id
1.starts with dGhvY
2.starts with bnZIZ
3.starts with dXZIb
Please find screenshot to get more idea
Related
I'm working on a private chat messenger application using react-native node and socket.io and need to set up the structure of the project and the data. the internet seems to be overloaded with ton of different answers and I was hoping to get some answers about the problems I have with these solutions
So as far as I understand from the official socket.io documentation
https://socket.io/docs/rooms-and-namespaces/#Namespaces
namespaces were meant to "separate concerns within your application by introducing separation between communication channels" which sounds exactly like what I need for creating a separation between different conversation.
and rooms "Within each namespace, you can also define arbitrary channels that sockets can join and leave."
Here is the data structure I had in mind:
Conversations (namespaces) table -
id, namespace, user1, user2
Messages table-
conversationId, content, from(user1/2)
Here is the general flow I had in mind :
Step 1 -
User connect the main namespace ('/')
Step 2 -
The server returns a list of all user's conversations (namespaces) from the conversation table in the database
Step 3 -
Connect the user to (all?) his namespaces (or should I only connect him to the conversation he enters?)
Step 4 -
Join to default 'chat' room under each namespace (is this really necessary in a private chat application ? I know on slack for example it make sense because we speak to the same group of people under different channels, but is there any other advantage for room that I'm missing? and if it does necessary should the user join all the rooms belongs to him when the applications first fire or should I only join him into the room he enters?)
Step 5 -
When a user enters specific conversations he get all the chat history under this conversation (from Messages table in the database)
Step 6 -
When a new conversation is added we add a record to the conversation table (and we emit 'newConversation' event to the entire namespace/room that include just the sender for now (how do I make sure to notify the receiver about the new namespace related to him should I emit the event to the main name space and validate the access right on connection to namespace attempt or should I use a solution like #)) perhaps this is out of the socket.io scope and should be left to the server to deal with ?
Step 7 -
When a new message is sent we add it to the messages table and emit 'newMessage' event to the entire namespace/room
# on many places I saw some versions of the following solution:
How to send a message to a particular client with socket.io
which basically suggest storing a dictionary relating between each user and his socket id, what I don't understand is, every time a user refresh/login/logout he receive a new socket id and all the events we emitted to his previous socket id will now be lost
My expected result is a chat like whatsapp or facebook messenger (but with private conversations only) where you get notified in messages in other conversation even when you are on the home page and not inside a specific conversation
I am developing ionic mobile application with node js backend.
The problem is, I want to create dynamic topics with user's username or unique id to send data from server to mobile app.
what is the best way to do this task. any ideas or reference?
I found the solution for this task,
In the server created json array with name and the socket id.
Then every time new user connect to the server push new user to the array with respective socket id.
When emiting message go through the array, get socket id and emit to the specific user.
//sending to individual socketid
socket.broadcast.to(socketid).emit('message', 'for your eyes only');
you can find common socket commands from here:
credits to https://stackoverflow.com/a/10099325
Good afternoon,
I have a chat application written with NodeJS, Express and socket.io, in that application there are no rooms, when a user connects the socket (connection of that user) is linked to his id in an object that stays in memory:
Users = {
'1': {socket},
'2': {socket},
...
}
This works great! When a user is going to send a message it informs the recipient's id the server retrieves the equivalent socket and sends the message.
My problem is that due to the number of users I will have to upload a new server with the application, the problem is that users on different servers can not communicate because each server knows only the list of users that is connected to it. After much reading I came to the conclusion that the best way to solve this is by using the Redis Adapter, I looked for examples on the internet exhaustively however they all use Rooms to send the messages or send the same message to all the user and I would like to send one Message to a specific user regardless of which server instance it is if possible without the need to create rooms, simply recovering its connection based on its id and sending the message, would like to know if someone could tell me if this is possible and how to implement.
Below is an image of how my architecture should look:
I'm building an application in real time with socket.io
Each time the user enters the application creates a socket ID.
So if I joining the application in several browser tabs generates me a socket id for each tab.
I generate a room for specific users and included my sockets id,
but I need to emit broadcast message except to my sockets id.
This code don't work for me because emit broadcast message but sends to my other id sockets
socket.broadcast.to("room").emit('message', "somethings")
Any ideas?
Assuming you are using some application framework like express along side socket.io, you can use the valid session id obtained from Express.js for maping the socketIDs generated from the same session.
Other than maping the socketIDs to the sessionID, I dont think there is a way to obtain all the socketIDs generated across tabs as the information on other tabs wont be accessible in the current tab context.
Below are few existing questions discussing this same case:
manage-multiple-tabs-but-same-user-in-socket-io
socket-io-and-session
I understand routing (I think), but what's to stop User A from looking at User B's client-side source to discover the socket.io URL specific to each user and then build their own script to start listening for broadcast messages on that particular channel?
You would need to use something to determine who the client is. Once you do that, you could for example use the rooms feature, and only broadcast to the specific room where you put all your authorized users.
To identify the client, you could pass in cookies from the browser in a socket.io request, and compare that to any session data you have stored in Express/Connect/Whatever.