Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js and socket.io for a notification bar : Am I going the right way?

I'm currently coding a quite standard Apache/PHP/mySQL website using Symfony2 and considering using Node and socket.io for a special need : a notification bar. Nothing too fancy, a notification is lit when you've got a new friend request, a new mail...

I'm not too fond of periodically requesting my DB from an ajax loop; I'd like this feature to be fully scalable and to have a minimal footprint.

Thus I'm considering having a single periodical request on my DB, server-side, feeding my list of open sockets in Node with a push notification to each concerned user.

Am I going the right way ?

Cheers

like image 422
heyMcFly Avatar asked Jul 24 '13 15:07

heyMcFly


People also ask

How do you implement Socket.IO in node JS?

In order to do it, you need to create an index. js file and install socket.io and express. You can use the following command: touch index. js && npm install express socket.io && npm install --save-dev nodemon .

What is Socket.IO used for in the context of node JS?

Socket.IO is a library that enables low-latency, bidirectional and event-based communication between a client and a server. It is built on top of the WebSocket protocol and provides additional guarantees like fallback to HTTP long-polling or automatic reconnection.


1 Answers

You're definitely on the right track, but I suggest putting the notification stuff in your app, rather than polling your database. For example of friend request, do it when the friend request is sent:

function send_friend_request($from, $to) {
    $mysqli->whatever you do with that;
    send_notification($to, 'You have a friend request!');
}

where send_notification is a method that sends a POST request to your nodejs server. Your nodejs server receives the POST and forwards the message to whatever is listening to $to.

like image 57
Joe Frambach Avatar answered Oct 20 '22 05:10

Joe Frambach