Websockets were designed for fast bi-directional communication in the browser. Assuming you have control over the server and a native client (say an iOS or Mac app), are there any good reasons or situations to communicate over a websocket instead of using an HTTP library?
1 Answers
I'm going to answer a couple of different questions that hopefully ends up answering your question:
Is there a reason to use WebSockets from a client rather than HTTP? Yes.
WebSockets is bi-directional, full-duplex, low-latency and low-overhead compared to HTTP.
Part of the lower latency and overhead compared to HTTP/AJAX/COMET is that you don't have to re-establish the connection for each request.
Is there a reason to use WebSockets from a client rather than raw sockets? Yes.
The initial WebSockets handshake is HTTP server friendly (and has some origin and hash exchange safety mechanisms). It allows web servers to easily be updated to support redirect or proxy for WebSockets connections to the real WebSockets server application.
Another benefit of WebSockets is that it is a framed protocol which allows the application to focus on useful functionality without having to deal with its own framing and buffering.
Its fairly easy to add WebSockets support to existing TCP socket servers or proxy via something like websockify. Disclaimer: I made websockify.
From a server perspective WebSockets is a win because they can be accessed via standalone clients or via browsers. This means there are going to be more and more services that are currently TCP socket based that will be exposed via WebSockets. So adding WebSockets support to a client now will pay off in the future.

- 70,845
- 23
- 144
- 140
-
can you tell how a client may access the data payload of my websocket (which I can see in my google chrome dev tool)? – Dr. Younes Henni May 03 '18 at 10:39
-
In Chrome dev tools, select the Network tab at the top. Then select the WS sub-tab. On the left is a list of websocket connections from the page. Click on the one you want to view. The right pain shows information about the connection. By default it shows the headers, but if you select Frames at the top of that frame it will show the data for each frame. – kanaka May 03 '18 at 14:22
-
Yes, I knew that. I want to know if I can get the data displayed in the Fames to my local computer. And is there a way to access the data remotely? – Dr. Younes Henni May 03 '18 at 19:01
-
I don't believe that there is builtin support for this but this answer might be able to get you started: https://stackoverflow.com/questions/29953531/how-to-save-websocket-frames-in-chrome – kanaka May 04 '18 at 14:50