Simulating real time stock prices using web-socket

Amit Kumar
Xebia Engineering Blog
4 min readJun 27, 2021

--

After exploring how real-time updates like real-time stock prices, social feeds, notification, etc. from the server gets loaded on the client-side machine, I came to know that some technologies are used to accomplish this task like polling, server-side events, and WebSockets. In this blog, I am going to build a simple real-time stock price simulator using WebSockets in spring boot.

The WebSocket protocol adds a new capability for web applications that are full-duplex, two-way communication between client and server. It is used to fetch and send data between client and server and a connection once established between them can be used to fetch and send more data overtime in that single opened connection. An initial handshake (which is a one-time HTTP call) is made by the client with the server and when the server responds with 101 code then protocol upgrades with WebSocket protocol and both client and server can communicate with each other over this protocol with connection remaining open.

Unlike HTTP, which is an application-level protocol, in the WebSocket protocol, there is simply not enough information in an incoming message for a framework or container to know how to route it or process it. Therefore, WebSockets allow the use of sub-protocol. During the handshake, the client and server can agree upon the use of some sub-protocol…

--

--

Amit Kumar
Xebia Engineering Blog

Software Developer by Profession Avid Reader and Writer Understanding Life Fond of small moments in daily life