WebSocket is a communication protocol that provides full-duplex communication channels over a single, long-lived connection between clients and servers. Unlike HTTP, which is a request-response protocol, WebSocket allows both the client and server to send messages to each other independently at any time, enabling real-time data transfer.
Table of Content
Key Concepts of WebSocket
- Full-duplex Communication: WebSocket enables simultaneous two-way communication, where both parties (client and server) can send and receive messages concurrently.
- Persistent Connection: Once established, a WebSocket connection remains open, allowing for efficient exchange of data without repeatedly opening new connections.
- Low Latency: WebSocket reduces latency by eliminating the need for constant polling, making it ideal for real-time applications such as chat applications, live updates, and gaming.
How WebSocket Works
1. WebSocket Handshake:
The client sends an HTTP request to the server, requesting to upgrade the connection to WebSocket. If the server supports WebSocket, it responds with an HTTP 101 status code (“Switching Protocols”), indicating a successful upgrade to WebSocket.
2. WebSocket Connection Establishment:
Once the handshake is complete, a WebSocket connection is established over a single TCP connection. Both the client and server can now send messages to each other directly without the overhead of HTTP headers.
3. Data Exchange:
The client and server exchange messages asynchronously over the WebSocket connection. Messages can be sent in either text or binary format.
Features of WebSocket
- WebSocket Requests: Postman allows you to create WebSocket requests just like HTTP requests. This includes defining WebSocket endpoints and customizing headers.
- Message Sending and Receiving: You can send both text and binary messages through WebSocket connections in Postman and view the server’s responses in real-time.
- Response Validation: Postman supports assertions on WebSocket responses, enabling you to validate the content, status, or headers of WebSocket messages.
- Scripting: Utilize Postman’s scripting capabilities (using JavaScript) to manipulate WebSocket messages, perform dynamic validations, or handle complex workflows during testing.
Approach
- Open Postman WebSocket Request: Launch Postman and create a WebSocket request.
- Enter WebSocket URL: Input the WebSocket URL of the API to test.
- Send Messages: Transmit messages to the server and analyze responses.
- Verify Response Data: Use Postman’s response visualization to ensure correct behavior.
- Test WebSocket Events: Observe connection status and handle errors during communication.
Testing WebSocket APIs with Postman
Step 1: Create a New WebSocket Request
- Open Postman and click on “New” to create a new request.
- Select “WebSocket” as the request type.
Step 2: Enter WebSocket URL
- Enter the WebSocket URL you want to connect to (e.g., wss://echo.websocket.org).
Step 3: Send a Message
- Go to the “Message” tab within the WebSocket request.
- Enter a message you want to send to the WebSocket server (e.g., “Hello, WebSocket!”).
Step 4: Set Up Assertions
- Switch to the “Tests” tab in Postman.
- Write JavaScript code to validate the response received from the WebSocket server.
// Validate the response received from the WebSocket server
pm.test("Check WebSocket Response", function () {
// Parse the response data from the message received
const responseData = pm.response.text();
// Define the expected response message
const expectedMessage = "Hello, WebSocket!";
// Assert that the response message matches the expected message
pm.expect(responseData).to.equal(expectedMessage);
});
Step 5: Send the WebSocket Request
- Click on “Send” to execute the WebSocket request.
- Postman will establish a WebSocket connection, send the message, and receive a response from the server.
Example 1: Simple WebSocket Connection
- Open Postman and create a new request.
- Change the request type to “WebSocket.”
- Enter the WebSocket URL (e.g., ws://example.com/socket).
- Send a text message ({“action”: “hello”}).
- Observe the server’s response in the Postman console.
Example 2: WebSocket with Assertions
- Create a WebSocket request as before.
- Send a message and expect a specific response.
- Use Postman scripts to validate the received message format.
- Assert the response status or content.