Cryptocurrency Websocket Vs Rest

Cryptocurrency websocket vs rest

Difference between WebSocket vs REST:

With RESTful HTTP you have a stateless request/response system where the client sends request and server returns the response.

With webSockets you have a stateful (or potentially stateful) message passing system where messages can be sent either way and sending any given message is lower overhead than with a RESTful HTTP request/response.

The two are fairly different structures with different strengths.

The primary advantages of a connected webSocket are:

  1. Two way communication. So, the server can notify the client of anything at any time.

    So, instead of polling a server on some regular interval to see if there is something new, a client can establish a webSocket and just listen for any messages coming from the server.

    Cryptocurrency websocket vs rest

    From the server's point of view, when an event of interest to a client occurs, the server simply sends a message to the client. The server cannot do this with plain HTTP.

  2. Lower overhead per message. If you anticipate a lot of traffic flowing between client and server, then there's a lower overhead per message with a webSocket.

    This is because the TCP connection is already established and you just have to send a message on an already open socket.

    Bitcoin investment trust usa

    With an HTTP REST request, you have to first establish a TCP connection which is several back and forths between client and server. Then, you send HTTP request, receive the response and close the TCP connection.

    Facebook pre ipo financing

    The HTTP request will necessarily include some overhead such as all cookies that are aligned with that server even if those are not relevant to the particular request. HTTP/2 (newest HTTP spec) allows for some additional efficiency in this regard if it is being used by both client and server because a single TCP connection can be used for more than just a single request/response.

    If you charted all the requests/responses going on at the TCP level just to make an https REST request/response, you'd be surpised how much is going on compare to just sending a message over an already established webSocket.

  3. Higher Scale in some circumstances. With lower overhead per message and no client polling to find out if something is new, this can lead to added scalability (higher number of clients a given server can serve).

    Fetch Bitcoin Exchange Data For Beginners Using Websocket

    There are downsides to the webSocket scalability too (see below).

  4. Stateful connections. Without resorting to cookies and session IDs, you can directly store state in your program for a given connection.

    While a lot of development has been done with stateless connections to solve most problems, sometimes it's just simpler with stateful connections.

The primary advantages of a RESTful HTTP request/response are:

  1. Universal support. It's hard to get more universally supported than HTTP.

    WebSocket vs REST

    While webSockets enjoy relatively good support now, there are still some circumstances where webSocket support isn't regularly available.

  2. Compatible with more server environments. There are server environments that don't allow long running server processes (some shared hosting situations). These environments can support HTTP request, but can't support long running webSocket connections.

  3. Higher Scale in some circumstances. The webSocket requirement for a continuously connected TCP socket adds some new scale requirements to the server infrastructure that HTTP requests don't demand.

    Running a cryptocurrency exchange in australa

    So, this ends up being a tradeoff space. If the advantages of webSockets aren't really needed or being used in a significant way, then HTTP requests might actually scale better.

    Cryptocurrency websocket vs rest

    It definitely depends upon the specific usage profile.

  4. For a one-off request/response, a single HTTP request is more efficient than establishing a webSocket, using it and then closing it. This is because opening a webSocket starts with an HTTP request/response and then after both sides have agreed to upgrade to a webSocket connection, the actual webSocket message can be sent.

  5. Stateless. If your job is not made more complicated by having a stateless infrastruture, then a stateless world can make scaling much easier (just add more server processes behind a load balancer).

  6. Automatically Cacheable. With the right server settings, http responses can be cached by browser or by proxies.

    There is no such built-in mechanism for requests sent via webSockets.


So, to address the way you asked the question:

What are the pitfalls of using websockets in place of RESTful HTTP?

  1. At large scale (hundreds of thousands of clients), you may have to do some special server work in order to support large numbers of simultaneously connected webSockets.

  2. All possible clients or toolsets don't support webSockets or requests made over them to the same level they support HTTP requests.

  3. Some of the less expensive server environments don't support the long running server processes required to support webSockets.

If it's important to your application to get progress notifications back to the client, you could either use a long running http connection with continuing progress being sent down or you can use a webSocket.

Cryptocurrency websocket vs rest

The webSocket is likely easier. If you really only need the webSocket for the relatively short duration of this particular activity, then you may find the best overall set of tradeoffs comes by using a webSocket only for the duration of time when you need the ability to push data to the client and then using http requests for the normal request/response activities.