The WebSocket is a communication protocol, that makes it possible to open a two-way interactive communication session between the user’s browser and a server. With this, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.
Applications that require Real-time data streaming, Synchronization uses WebSocket primarily. Most browsers support the protocol, including Google Chrome, Microsoft Edge, Internet Explorer, Firefox, Safari, and Opera.
The WebSocket protocol specification defines
ws (WebSocket) and
wss (WebSocket Secure) as two new uniform resource identifier (URI) schemes that are used for unencrypted and encrypted connections, respectively.
Web Socket Server: A WebSocket server is nothing more than an application listening on any port of a TCP server that follows a specific protocol. The task of creating a custom server tends to scare people; however, it can be straightforward to implement a simple WebSocket server on your platform of choice. In Delphi, we can create our own server directly inheriting from Indy
TIdCustomTCPServer. WebSocket servers are often separate and specialized servers (for load-balancing or other practical reasons), so you will often use a reverse proxy (such as a regular HTTP server) to detect WebSocket handshakes, pre-process them, and send those clients to a real WebSocket server.
Web Socket Handshake: The server must listen for incoming socket connections using a standard TCP socket. Depending on your platform, this may be handled for you automatically. For example, let’s assume that your server is listening on
example.com, port 8000, and your socket server responds to
GET requests at
Client HandShake: Even though you’re building a server, a client still has to start the WebSocket handshake process by contacting the server and requesting a WebSocket connection. So, you must know how to interpret the client’s request. The client will send a pretty standard HTTP request with headers that looks like this (the HTTP version must be 1.1 or greater, and the method must be
GET /chat HTTP/1.1
When the server receives the handshake request, it should send back a special response that indicates that the protocol will be changing from HTTP to WebSocket. That header looks something like the following (remember each header line ends with
\r\n and put an extra
\r\n after the last one to indicate the end of the header):
<strong>HTTP/1.1 101 Switching Protocols</strong>
Exchanging Data formats : Message is Exchanged in certain formats Between client and server called “Frames”. For more information check here.
Existing Solutions Available :
Check the video for creating web sockets server and client implementation in Delphi below.