5

I am going to build a web application to manage notes (think of something similar to Evernote). I have decided to use Backbone.js+JQuery client side. Server-side, I haven't decided yet: either pure PHP (that I know very well) or Node.js+Socket.io (completely new to me).

I am considering Node.js+Socket.io because I would like to have my web application being real-time (i.e: if a user updates a note, that note gets updated instantly for a collaborator who shares that note, without reloading the page).

I was also considering, as a third alternative, to use Node.js and Socket.io for the UI and PHP for the REST API (I feel more comfortable to build an API with PHP). The PHP and Javascript code will share the MongoDB database.

My question is this: if I develop the REST API for my web application with PHP and a new note for the user gets created through the API (i.e.: the Android app sends an API request to create that note), will Node.js, Socket.it and Backbone.js be able to instantly update the UI of the user and show the new note on their screen? I think that can be called "push notification".

I hope I was clear enough.

Also, is there any alternative outstanding technology to build real time web applications?

Dan
  • 15,948
  • 20
  • 63
  • 92

2 Answers2

9

Yes Node.js + Socket.IO will do a very good job of this. Node uses an event-loop, this means upon a request it is entered into a queue. Node deals with these requests one-by-one. Traditional web servers deal with a 'Thread-per-request' approach where a thread is created to handle that requests.

The benefit of Node here is that it doesn't need to context switch so often, this means it can deal with these requests very quickly... most likely faster than your PHP server. However Node runs as a single process, on a single CPU core. If your application is CPU intensive it could be that it blocks, meaning the time for each requests will be slower.

However it sounds to me like your application isn't CPU intensive, meaning Node.js will work well.

Decision If your time is limited, and you don't want to learn a new skill (Node), PHP will be fine. If you have the time I recommend learning Node.js, as it is very strong when it comes to I/O intensive tasks such as a REST API for creating Notes.

Updating the UI If your intended use is through a mobile device, I recommend using WebSockets but having a fallback such as long-polling. It is possible to update the Client UI using either Node or PHP. However from my experience it is much easier to do so using Socket.IO on Node.js.

Example Updating the client using Node.js / Socket.io

Client-side

  socket.on('new-note', function (data) {
    placeNewNote(data);
  });

Server-side

socket.emit('new-note', data);

Getting Started With Node: How do I get started with Node.js

Please also note, if you want to build a native Android mobile app which uses WebSockets... you will need to use: Java socket.io client

Community
  • 1
  • 1
Jack
  • 15,614
  • 19
  • 67
  • 92
  • Jack, thanks for your answer. And what about real-time? Will a change through the REST API implemented with PHP be reflected in the UI straightaway? – Dan May 10 '12 at 09:52
  • 1
    I've added some information about updating the UI. My final year project at university was very similar to what you're creating... I used Node.js / Express / Socket.IO and stored the 'Notes' in a MongoDB database. – Jack May 10 '12 at 09:56
  • Is Express a replacement for Backbone.js? – Dan May 10 '12 at 09:58
  • 1
    Express is on the server-side, that client-side code is an example of a websocket listener. Upon the client recieving the message called 'new-note' it will call that function.. which you could re-write to place a note on the client. – Jack May 10 '12 at 10:01
2

Using Node.js for both web server and push server is of course the best way. Especially since if you are going to use Node.js anyway then you have to learn it, so learning how to make a web server is only natural (I advice using the most famous Express framework).

Now you can use PHP for web server and Node.js for push server. To make them communicate with each other you would probably want to add Redis to your application. Redis will allow you to push notifications to any client connected to it, like PHP server or Node.js push server (and it scales well). From that point push server will push the data further to the client's browser.

Alternative technology would be for example Twisted server. Of course you'll need to learn Python in order to use it. And I don't know whether it supports WebSockets correctly. I think you should stick with Node.js + socket.io.

freakish
  • 54,167
  • 9
  • 132
  • 169
  • Thanks freakish. So you are saying that if I take PHP out of the equation and build on Backbone, Node, Socket.io I do NOT need Redit in order to have a real time web app. Is that right? – Dan May 10 '12 at 09:55
  • IS Express a replacement for Backbone.js? – Dan May 10 '12 at 09:59
  • 1
    @dan Yeah, technically you don't need it. But you should use it anyway, since socket.io is unable to scale to many machines without it (or at least I do not know the other way to scale it). So you should use Redis, unless you assume that one machine will be enough for your app. But you should not make such assumptions. :) – freakish May 10 '12 at 10:02
  • 1
    @dan Backbone.js is a client-side library, while Express is a server-side framework for Node.js. Express is like Django for Python. :) – freakish May 10 '12 at 10:04
  • freakish, regarding the one-machine assumption, I would like to start as simple as possible as I am going to use a lot of new technologies for me. If I start developing just for one machine, would it be difficult to user Redis later on if my web app will get a lot of users? – Dan May 10 '12 at 10:15
  • 1
    @dan Well, it depends. Adding Redis means only installing it and adding 4-5 lines to the socket.io configuration. But the problem is when you for example hold data in Node.js app's memory. Such code needs to be rewritten so it uses socket.io storage. For example every `socket` object has methods `.get` and `.set` which should be used for retrieving/storing (respectively) additional data for a given socket. If you use them, then everything will be fine. – freakish May 10 '12 at 10:29
  • @freakish "Adding Redis means only installing it and ..." not really, it has a considerable overhead on the machine(s) it is running on and is not really memory tuneable (unlike Apache & MySQL). – Julian Knight May 11 '12 at 14:13