Table of contents
1.
Introduction
2.
What is WebSocket Protocol?
3.
How to create a WebSocket Request in Postman? 
4.
How to send Messages?
5.
Saving Messages
6.
Additional Message Properties when using Socket.IO
7.
Difference between Raw WebSocket and Socket.IO
8.
Frequently Asked Questions
8.1.
Does Socket.IO use WebSockets?
8.2.
What is WebSocket API?
8.3.
Why are WebSockets used?
9.
Conclusion 
Last Updated: Mar 27, 2024

Using WebSocket Requests in Postman

Author Neha Chauhan
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Postman is an API development platform for designing, building and testing API (Application Programming Interface). It can create different HTTP requests (GET, POST, PUT), save environments for later use, and convert the API to code for various languages (like JavaScript, Python).
 

Using WebSocket Requests in Postman

WebSocket API is a technology that allows two-way communication between the user’s browser and the server. With the WebSocket API, after the beginning of the communication channel, we do not need to close the connection until we want to stop the communication. 

In this article, we will discuss what a WebSocket API is and how to use them with Postman.

What is WebSocket Protocol?

The WebSocket protocol allows data exchange between a client and server over a persistent connection. By ‘persistent connection’, we mean that once the connection is established between the client and the server and the communication has begun, the connection will remain established unless we want to stop the communication between the client and the server. 

In the HTTP protocol, the client will send a request, the server will send a response and the connection will be closed. For every new request, a new connection will be established. 

The WebSocket protocol will keep the connection alive until the requirement is fulfilled. Many requests and responses will be exchanged between the client and the server by reusing the connection. The connection is two-way; communication can happen from both directions. 

Websocket protocol

Yay! Now that you have developed a basic understanding of WebSocket Protocol, let’s learn how to use a WebSocket connection in Postman.

How to create a WebSocket Request in Postman? 

To create a WebSocket Request, head over to your Postman Desktop App. 

(If you have not installed Postman Desktop App, we recommend you read this article How to Install and Setup Postman and install the App.)

 

🚀 Click on New

New button img

 

🚀 Select the WebSocket Request

Request img

 

🚀 Notice that there is a Drop down list with two options - Raw and Socket.IO, for this example we will send a Raw WebSocket Request.

We will discuss the difference between Raw and Socket.IO further in this article. 

🚀 Enter the URL - wss://ws.postman-echo.com/raw

Img 3
 

Remember that WebSocket Requests start with ws:// or wss:// where ws stands for WebSocket and the ‘s’ in wss (like the ‘s’ in https) means secure WebSocket connection.  

wss://ws.postman-echo.com/raw will echo the message sent.

🚀Click on Connect to establish a connection.

Yay!🤩You have successfully established a connection. 

Output img

To disconnect your WebSocket connection, click on Disconnect.

How to send Messages?

Let’s use wss://ws.postman-echo.com/raw and send a Hello World! message. 

🚀 Type the message in the Message Box and click on send.

Output img


🚀 The output will be 

Output


🚀 The upward arrow ⬆️ is the request sent and the downward arrow ⬇️ is the response received. 

You can send Text, JSON, XML, HTML or Binary messages. 

🚀Let’s send a JSON message. From the drop-down list, select JSON.

Output img

Similarly, you can send many more messages. 

Yay!🤩 Now you know how to create a connection and send messages.

Saving Messages

To save the message, you must first Save the request to a collection.  

Note: You cannot save a WebSocket request to a collection that has already been used to save an HTTP request. Always create a separate collection for WebSockets. 

🚀 Save the request in a collection by Using the Save button. 

Output img
 

🚀 After saving the Request, save the message by clicking on ‘Save Message’. By default, the message will be saved as ‘New Message’ you can click on and select ‘Rename’ and rename the saved message. 

Additional Message Properties when using Socket.IO

🚀 We can add an event name to each request sent using Socket.IO. Enter the event name next to the Send button. By default, the event name is messages. 

Let’s name the event ‘Greeting Message’.

Output img


🚀 We can add arguments by clicking on the +Args next to the message type drop-down list. 

Output img

🚀The event's name and arguments help listen to particular events. 

Type the name of the event on the Listeners tab and click “+” to add it. Since this is Socket.IO, you’ll only receive events for which you’ve added listeners. 

Output img

Difference between Raw WebSocket and Socket.IO

Raw WebSocket is a technology that provides a full two-way communication channel.

Socket.IO is a library built on WebSocket technology. 

Socket.IO is easier to work with. If a connection is lost, it will be automatically reconnected if we use Socket.IO but in WebSocket, we will have to reconnect the connection manually. 

Raw WebSocket runs only two requests, the GET request and the UPGRADE connection, but Socket.IO mostly runs four requests, the GET request, the Socket.IO client library, the three long polling AJAX requests and the UPGRADE connection. 

Running so many requests is a waste of the network, hence we can say that Raw WebSocket is less network consuming. 

Yay!🤩We have discussed the differences between the WebSocket and Socket.IO also. 

Frequently Asked Questions

Does Socket.IO use WebSockets?

Yes. Socket.IO is a library built upon WebSockets technology and some other technologies. 

What is WebSocket API?

WebSocket API supports two-way communication between client and the server. Once a connection is established between the client and server, it will not be lost unless any one of the client or server decides to stop the communication. 

Why are WebSockets used?

WebSockets are used for real-time communication like for a trading website the data changes every second and needs to be updated accordingly or like for a chatting application, we have no idea for how long the connection should be established and how many messages will be exchanged within the conversation.

Conclusion 

Pat yourself on the back👏 for finishing this article. We discussed what WebSockets are and how to send requests using them in Postman. We discussed creating a connection and sending messages using both Raw and Socket.IO. Lastly, we discussed the difference between Raw WebSocket and Socket.IO.

After reading this article, if you are interested in knowing more about Postman, we recommend you to read some of our articles -

🔥 Soap Requests in Postman

🔥 Managing Environment in Postman

🔥 Postman Interceptor

🔥 Postman Scripts

Head to the Guided Path on the Coding Ninjas Studio and upskill in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more courses.

If you want to Practice top Coding Problems, attempt mock tests, or read interview experiences, head to the Coding Ninjas Studio, our practice platform.

We wish you Good Luck!🎈 Please upvote our blog 🏆 and help other ninjas grow. 

Live masterclass