Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async RestFul vs Websocket

I am implementing a Client-Server API with long job processing times (order of minutes in some cases). Some of api calls are short and respond immediately but a coupe require some back-end processing. I am using node.js as the web server. My current implementaion is as follows -

Client(browser) <-> node js <-> engine

The engine is a back-end process which processes each job (C++ code). All the interactions are http. Now, traditionally I would implement the long jobs as async ajax/restful requests and short jobs as sync restful requests.

I am going to have status updates for the long processing jobs (processing large data) - like intermediate results, percent complete etc.

I am loolking at WebSockets as an alternative (and I am relatively new to it). Here are my questions -

  1. Should I look into websockets for the long jobs instead of the async restful api (I would love to avoid handling client-timeouts, long-polling etc.) ?
  2. How about moving all the requests to websockets (why bother with rest at all? )
  3. In general, any best practices to implement this architecture. (Previously, I worked on projects with the interaction between the webserver and engine as a simple TCP connection with custom commands.)

NOTE:- I am not worried about cross-browser support (especially the older versions) right now.

like image 575
vsky Avatar asked Aug 03 '26 17:08

vsky


1 Answers

I've implemented a Client-Server API using websockets to communicate between a browser and a C++ backend. The library we used was libwebsockets http://git.warmcat.com/cgi-bin/cgit/libwebsockets/

Both long running and also simultaneous commands worked very well on the websockets. Multiple requests can be sent from the client and the server can respond when it is ready and can send responses back out of order (or coalesce responses into a single response).

The timeout & long-polling stuff necessary with Ajax becomes simpler e.g. client disconnections can be detected by the server when the socket is broken.

As for best practices, I based my design on these articles. We used JSON to encode the messages.

  • http://www.altdevblogaday.com/2012/02/01/controlling-your-game-engine-over-websocket/
  • http://www.altdevblogaday.com/2012/01/23/writing-your-own-websocket-server/

If your server is node.js you could look into Socket.io. This abstracts the communication layer and can pick between ajax, websockets etc depending on what's available.

like image 68
Gerard Condon Avatar answered Aug 05 '26 08:08

Gerard Condon



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!