Dynamic topics with socket.io - node.js

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

Related

How to send message to specific user in socket.io?

I am creating a chat application in Socket.io and node I am new in this I have a some query regarding chat app how can we maintain user's list and how can we send a message to specific person from user list?
Like this (On the server-side) Cheatsheet:
// sending to individual socketid (private message)
socket.to(<socketid>).emit('hey', 'I just met you');
The <> chatacters can be omitted, just make sure you replace socketid with whatever the socket ID truly is. This will sent a message to ONLY the socket id specified. I know this works because I use it.

How to use socket.io-redis Nodejs

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:

Socket.io - Emit broadcast message except my sockets id

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

How to send real time notification to single user using node.js and PHP

I am trying to integrate real time notifications with Node and socket.io in a Symfony Application. I have read a lot of information about this topic and have a working Node application.
nodeClient.js
var socket = io.connect( 'http://192.168.15.106:8080' );
$('a.sendSmile').click(function(){
socket.emit( 'message', { name: 'something' } );
});
socket.on('message', function(data){
console.log(data.name);
});
The problem now is with the above which is working perfectly I am able to send real time notification to all the users at once. But what's the best way to target a single user?
For example a user can send a smile to another user so only the second user should receive the notification and not all the users.
Should I make multiple listeners for node? or any other method to do this?
You need some way of identifying which socket that connected to your server is the one you want to send data to and then you can send to just that socket. You can keep track of user names when users connect or if you have some auth system, you can keep track of which socket belongs to which authenticated user.
Your server holds a list of connected sockets. Each connected one at a time and triggered a connection event on your server when they connected. Your application needs to create a way of knowing which of those connected sockets you want to send the data to. This is not something you've described anything about how you want that to work so we can't really help more specifically.
You can dispatch a notification to single user if you can discriminate that user. For example you can get a user_id on client connection to your nodejs server (the user_id is send from client, inside message) and save it in a key-value store (like Redis, memcache, ...). In this way you can correctly dispatch the notification, arrived from the server (like Symfony2 application), to right user.
I suggest you use Redis, both as a key-value store and for its implementation pattern of the publish/subscribe usable as a channel of communication between the server and the application of realtime.

socket.io chat with private rooms

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
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:
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).
When a Bob wants to talk to Jeff, send the server an event stating such.
The server looks up Jeff's Socket.IO connection in the object from step 1.
The server uses this connection to send Jeff (and only Jeff) the private message.
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.

Resources