Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are Node.js+Socket.io+MongoDB webapps truly asynchronous?

Tags:

I have a good old-style LAMP webapp. A week ago I needed to add a push notification mechanism to it.
Therefore, what I did was to add node.js+socket.io on the server and poll the MySQL database every 10 seconds using node.js to check whether there were new items: if so, I would have sent them to the client(s) with socket.io.
I was pretty happy with the result, even if that is not a proper realtime notification (as there is a lag of up to 10 secs).

Now, I am about to build a new webapp which will need push notifications, too. I am wondering whether to go with the same approach as the first one (that I believe is more stable and mature) or to go totally Node.js, without PHP and Apache. As for the database, I have already decided to go for MongoDB.

Finally, my question is: if I go for Node.js+Socket.io+MongoDB will I get a truly near-real-time webapp? I mean, as soon as a new record is inserted into MongoDB, will there be some sort of event triggered that I can catch via node.js, do some checking on it and, if relevant, send the notification to the client? Or will there be anyway some sort of polling on the db server-side and lag, as with my first LAMP webapp?

A related question: can you build a realtime webapp on MySQL without doing any polling as I did with my first app. Or do you need MongoDB (or Redis)?

I hope this question is not too silly - sorry, I am just starting with Node.js and co.

Thanks.

like image 243
Dan Avatar asked Oct 19 '12 16:10

Dan


People also ask

Is Socket.IO asynchronous?

JS, Socket.IO enables asynchronous, two-way communication between the server and the client. This means that the server can send messages to the client without the client having to ask first, as is the case with AJAX.

Is Socket.IO scalable?

Socket.IO servers can be replicated and scaled horizontally, to provide fault-tolerance (ie. if one server fails, your service would still work) and serve more users (ie. by adding more server capacity).

What is the use of Socket.IO in 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.


2 Answers

I understand your problem because I switched to node.js from php/apache/mysql too.

  • Generally node.js is stable, modules and your scripts are the main reasons for errors

  • Real-time has nothing to do with database, it's all about client and server, you can query as many data as you want in your requests and push it to the other client.

  • Choosing node.js is very wise but it's harder to implement.

  • When you insert a new record to your db, the event is the request itself, you will make a push event along with the database query something like:

    // Please note this is not real code, just an example of the idea app.get('/query', function(request, response){     // Query your database     db.query('SELECT * FROM users', function(rows){           // Push notification to dan          socket.emit('database_query_executed', 'to_dan', rows);            // End request          response.end('success');       })    }) 
  • Of course you can use MySQL! And any database you want, as I said real-time has nothing to do with databases because the database is in the middle of the process and it's totally optional.

  • If you want to use node.js for push notifications and php/apache for mysql then you will need to create 2 requests for each server something like:

    // this is javascript ajax('http://node.yoursite.com/push', node_options) ajax('http://php.yoursite.com/mysql_query', php_options) 

    or if you want just one request, or you want to use a form, you can call your php and inside php you can create an http or net request to node.js from php, something like:

    // this is php new HttpRequest('http://node.youtsite.com/push', HttpRequest::METH_GET); 
like image 95
Adam Halasz Avatar answered Sep 27 '22 21:09

Adam Halasz


Using:

  • A regular MongoDB Collection as the Store,
  • A MongoDB Capped Collection with Tailable Cursors as the Queue,
  • A Node worker with Socket.IO watching the Queue as the Worker,
  • A Node server to serve the page with the Socket.IO client, and to receive POSTed data (or however else the data gets added) as the Server

It goes like:

  1. The new data gets sent to the Server,
  2. The Server puts the data in the Store,
  3. The Server adds the data's ObjectID to the Queue,
  4. The Queue will send the newly arrived ObjectID to the open Tailable Cursor on the Worker,
  5. The Worker goes and gets the actual data in the ObjectID from the Store,
  6. The Worker emits the data through the socket,
  7. The client receives the data from the socket.

This is 'push' from the initial addition of the data all the way to receipt at the client - no polling, so as real-time as you can get given the processing time at each step.

like image 45
floatingLomas Avatar answered Sep 27 '22 21:09

floatingLomas