i want to build a push notifications service like facebook with nodejs and socketio. i have a question:
- how server can know what exactly client to send notification when many client connected to server?
Thanks for help!
Associate sockets with users when they connect, so when you have to notify user "Test" just look at the user.sockets[] list and send him whatever info you want to send.
In other words, Node.js doesn't have to "know" about users, you should take care of that and talk with Node.js in "socket" terms.
You can use socket.io's room feature. You can subscribe each client to its unique room and then emit an event in that room so that user will be the only one to get that.
Related
I am trying to implement private messaging with socket.io for my mobile applications which have a direct messaging feature like Instagram. Right now, I am using Node.js and React Native. I am kinda new to socket.io. I saw many examples of that. However, one thing is not clear in my mind.
User clicks "send message" button. Then I create a socket connection and the user joins a room with socket id. Then user sends a message to that room.
The problem here is, how other user will get the message? Because at this point, I don't think other user knows the room id. Of course if there is a better solution for that, I am open to every suggestion.
One thing you can do is create a room for each person. When the person logs into your app and connects with socket.io, you'll want to have them automatically join the room with their user id.
Then when someone wants to send them a message, they can just send the message to the room for the receiving user.
However, I think if you are building a messaging app, socket.io is not the right way to go. As far as I know you can't listen on sockets while the app is in the background (and even if you could, it would drain your users' battery life). You should use push notifications instead and use the data field (e.g. zo0r/react-native-push-notification and firebase).
I was curious to know that can we implement push notification to our client via our Node.js server just like we send emails using nodemailler in Node.js? If yes! Then how? Can anyone please briefly elaborate. Also a point to note that the push notification should be user specific just like our mails are which are distinguished by our mail id.
Thanks in advance.
You can achieve this using Firebase Cloud Messaging. Here's a high level overview:
Each client registers with FCM and sends it's unique token to your node.js server.
The server saves this token and associates it with the user.
When it comes time to send the push notification, you use the FCM node.js library to send a push notification to the token associated with the user you want.
Not sure if it is the best way.
You might open a port on server side and listen to that port from client side. I am talking about socket programming. New notifications will be sent through that open port.
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.
I am an inexperienced developer, i just started learn node.js socket.io.
I want to make notification for each user if there's post in their wall or someone has replied(commented) to the user's post, just like in the fac*book/go*gle+ notification.
I follow the tutorial here http://www.codesizzle.com/node-js-and-socket-io/
I'm combining it with laravel4 and redis as the handler that publish the message/notif to redis, then nodejs will listen to redis and if there's post/event then send it using socket io to user's browser
Based on my understanding, example:
userA posts something to userB
userB receive the notif that says "user A has posted something on your wall"
to achieve this:
I need to make userB subscribes to its own channel, maybe using his id
then when userA post something, laravel4 will receive the ajax post data
laravel insert the post to MySQL and publish the user's id + his post to redis
after that nodejs will receive the event and publish it to userB's browser.
my question is in my statement "userB subscribes to its own channel, maybe using his id." this means userB will subscribe to channel maybe notif-channel-{userId} -> notif-channel-967 , if i have thousands of users then i will have thousands of different channel, maybe from notif-channel-1 until notif-channel-50000,
will it become a problem?
what is the best practice?
does the flow already correct?
thankyou
note :sorry for the bad english.
UPDATE
After reading some more articles, i think i change the flow to:
UserA posts somethingn on UserB's wall
Then send ajax post to laravel so i can put the message/post into the db
After that emit/push event to socketio thats working on nodejs server to handle the event and push it to userB's channel, tell userB that userA has posted something
If success then notify UserA that his posts is successfull
notice that i dont involve redis in the above flow, any feedbacks?
Using sockets to do this is a pretty complicated way of doing things. It's possible, but all you really need to do is add a timed post request from that page (client side) to check for new comments.
All you really need at that point is in the db. From, To, Date, Message.
With sockets you'd have to set up a channel for each user that has a 'wall', and anyone that is viewing a persons 'wall' will have to subscribe to it, and if it's a lot of people, you will have issues when you need to expand. That means using redis with sockets.io.
While that's 'cool' to do, it's overkill in my opinion.
Build a microservice which has socket connections using caching systems and even data warehousing techniques, and at database level use triggers, when data is crud
Use parse db server which has cool feature of subscribing to live query.
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.