REST API or websocket?

I'm working on a webapp and mobile app that need real time events (for exemple a chat where I need to push to an other client). I work with node, mongodb, angular, phonegap.

I have begin with a mix of REST API and events via socket.io but I'm thinking to transform it in a 100% websocket app.

I known that the development will be a little more difficult but what would be the best solution for you?

Thanks in advance !

Asynchronous technology is definitely the way to go if you want to support large number of clients. Async gives the ability to the server to send data to the clients at any moment. Rest is only client to server. So you need to poll which is really costly and inefficient.

Websocket is also better as it uses less data on the network. It does not rely on HTTP after connection. A TCP connection is actually established. If there are some proxies, you may have to be careful.

Websocket alone is a simple wire with no added protocol other than send/receive. So you need something above to manage channels and subscriptions. STOMP is usually used for that. You may look at stomp.js.

You may also consider SSE. It is less complicated than web-socket, relies on HTTP, so more chatty. Well web-socket is not really complicated, there are libs to help.

You may also mix websocket for part of your api and keep REST for another part. You have to evaluate what makes sense.

Some resources:

You might be interested in WAMP (http://wamp.ws/), which provides flexible application messaging on top of WebSocket:

  • Remote Procedure Calls (roughly corresponds to REST)
  • Publish & Subscribe (for real-time push / notifications)

Disclaimer: I am original author of WAMP and work for Tavendo.

Well, its matter of usage. Long back when I started with NodeJS, I had chance to use both SocketIO and ExpressJS.
So I can suggest use right tool for right thing. If its mean to be realtime such as sending message, chat etc SocketIO is good, but if requirement is simple to access some resource over network, that don't really need realtime implementation, use basic REST api using ajax or something at client side. It also helps in catching the data hence less load on server.

You can implement both stack together i.e. SocketIO and some RESTful implementation lib in nodejs app.