Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST API or websocket? [closed]

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 !

like image 303
Jowy Avatar asked Dec 30 '25 08:12

Jowy


1 Answers

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:

  • http://caniuse.com/#feat=websockets
  • http://caniuse.com/#feat=eventsource
  • https://github.com/jmesnil/stomp-websocket (I did not test it but there are quite some stars so it should be ok :-) )
like image 146
unludo Avatar answered Jan 01 '26 20:01

unludo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!