Notifications in Pega – Part 1 – WebSocket introduction
In this blog article, we will start with some basics about WebSocket technology.
First, letโs talk about the Client-Server model
You can see the client-server model in every service provider architecture. There can be one or more servers serving one or more clients.
When we see from Pega perspective, the application server or the web server is the SERVER and the browser through which we log in Pega application is the CLIENT. There can be multiple clients (multiple operators) accessing the server (Pega application).
Now all these data are requested by the client. When we log in from the browser, HTTP requests are sent and the HTML content from the server is sent as web pages to the client.
So, everything you see is ask and give. The client asks the server and the server provides the server with the necessary data.
As a client, do we need to ask for data every time from the server? Think of use cases like real-time data โ Tracking cab, packages, newsfeed or gaming, and chat applications. Can the server be proactive and send the data to the clients? Yes, they can. WebSocket CAN be a solution ๐
Letโs talk about some basic differences between HTTP and WebSocket
What are the main differences between HTTP and WebSocket?
WebSocket is not a replacement for HTTP but is an extension.
HTTP are usually half-duplex communication, whereas WebSocket supports full-duplex communication.
What is half-duplex and full-duplex communication?
Half-duplex โ It means, the server does not push any data. You need to request the server for any data.
Think of a chat application, let’s see in the picture how it would behave if it used half-duplex and full-duplex communication.
WebSocket provides high performance and low-latency communication than HTTP, How?
For WebSocket, the connection is opened once with a handshake is kept open for server-client communication and can be close any time. So, latency is very low, server can just keep pushing data. Whereas in HTTP, you need to form an HTTP request (heavy) every time for the communication.
WebSocket uses the schema ws:// similar to http:// or https://
What ports does WebSocket use?
WebSocket is designed to work over the default HTTP ports โ 80 and 443 (for TLS connections). In this way, you donโt need to take special care on WebSocket connections like firewall blocking connections etc.
Okay, Now letโs go one step deep into WebSocket technology ๐
What is WebSocket?
– WebSocket is a bidirectional full-duplex protocol, used in client-server communication similar to HTTP protocol.
– It is a stateful protocol โ which Means, the connection between server and client will be kept alive until it is terminated manually but either the client or server. The connection is a single TCP connection.
– It is mostly used in real-time data applications, gaming and chat applications.
The steps in establishing the WebSocket connection is as follows
Step 1: The process begins with sending a normal HTTP request to the server.
The header contains the request for a WebSocket connection.
Step 2: The server responds with a handshake by establishing the connection.
Step 3: Once the connection is established, then messages can be pushed into the connection.
WebSocket works with API events. The following events are used
Open โ Once the connection is established, an event onopen is fired, to say that the connection is open
Message โ This event happens when the server wants to send some data. โ onmessage event
Close โ This event onclose marks the end of connection between server and client
Error โ When some error occurs โ onerror is raised followd by close event.
JavaScript applications use the WebSocket technology effectively for server-client communication.
Enough of theory, letโs see practically how the WebSocket connection is established.
Important note: Pega started supporting WebSocket connections from Pega 7.3. In prior versions long-polling is used for these message communications.
How to check if Pega uses a WebSocket connection?
Step 1: Launch the designer studio in Chrome browser.
Step 2: Go to browser developer tools and click on the networks tab โ WS
Step 3: Now just refresh the browser and see the magic ๐
You see an websocket open connection established.
Step 4: Click on the name, to check more content
Headers tab โ You see, it uses the PRPushServlet to establish the WebSocket connection.
You will see the servlet configuration in the web.xml
Status code – green
Click on the Messages tab.
You will see some messages. These are corresponding to heartbeat checks (default). Green signifies the messages sent to the server (upload) and red is the message from the server to the client (download)
This will keep on going every 10 seconds.
Initiator tab
Here you will the Java script files responsible for initiating the WebSocket connection.
So, what we understood is
Whenever you log into the Pega application, the WebSocket connection is established by default. You are all ready to send and receive messages using WebSocket.
Hope you understood some basics about WebSocket and its usage in Pega.
In the next article, we will see how we can push messages (notifications) to the Pega browser clients using WebSocket ๐