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 -
NOTE:- I am not worried about cross-browser support (especially the older versions) right now.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With