WebSockets vs Rest API for real time data? Which to choose?

Technology CommunityCategory: Software ArchitectureWebSockets vs Rest API for real time data? Which to choose?
VietMX Staff asked 3 years ago
Problem

I need to constantly access a server to get real time data of financial instruments. The price is constantly changing so I need to request new prices every 0.5 seconds. Which kind if API would you recommend?

The most efficient operation for what you’re describing would be to use a webSocket connection between client and server and have the server send updated price information directly to the client over the webSocket ONLY when the price changes by some meaningful amount or when some minimum amount of time has elapsed and the price has changed.

Here’s a comparison of the networking operations involved in sending a price change over an already open webSocket vs. making a REST call.

webSocket

  1. Server sees that a price has changed and immediately sends a message to each client.
  2. Client receives the message about new price.

Rest/Ajax

  1. Client sets up a polling interval
  2. Upon next polling interval trigger, client creates socket connection to server
  3. Server receives request to open new socket
  4. When connection is made with the server, client sends request for new pricing info to server
  5. Server receives request for new pricing info and sends reply with new data (if any).
  6. Client receives new pricing data
  7. Client closes socket
  8. Server receives socket close

As you can see there’s a lot more going on in the Rest/Ajax call from a networking point of view because a new connection has to be established for every new call whereas the webSocket uses an already open call. In addition, in the webSocket cases, the server just sends the client new data when new data is available – the client doens’t have to regularly request it.

A webSocket can also be faster and easier on your networking infrastructure simply because fewer network operations are involved to simply send a packet over an already open webSocket connection versus creating a new connection for each REST/Ajax call, sending new data, then closing the connection. How much of a difference/improvement this makes in your particular application would be something you’d have to measure to really know.