Let’s start!
So, what is SocketIO? It is a library which allows you to do event-based communication between client and server.
Today, we’ll see how to connect and test SocketIO with Postman.
Setting up Server and SocketIO
First, you’ll need to set up SocketIO on the server side. You can find the installation step here.
I’m using the express framework for setting up our server. You can find the example code below. As the goal is to test SocketIO on Postman I’m not going deep into how to set up the server.
import express from "express";
import { createServer } from "http";
import { Server } from "socket.io";
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer);
io.on('connection', (socket) => {
console.log('socket connected');
});
httpServer.listen(3000, () => {
console.log('server started');
});
Now we have to add socket events that will be sent from the client-side i.e. React, Angular etc. Sending events is called emitting it can be done from both sides as the socket allows bi-directional communication. You can send an event by socket.emit() method. See the below example:
io.on('connection', (socket) => {
console.log('socket connected');
socket.emit('test', 'Hello Testing event!');
});
To receive events on the server-side socket.on() method is used. I have created an event addition which accepts two arguments, performs addition and send back a response containing the addition of numbers passed in arguments. See the code below for example:
io.on('connection', (socket) => {
console.log('socket connected');
socket.on("addition", (arg1, arg2, callback) => {
console.log({arg1, arg2});
callback({
sum: Number(arg1) + Number(arg2)
});
});
});
Now, our server is ready to receive an event.
Setting up the Postman
Now comes the second part of our journey, Postman where we will test the server we created earlier. Follow the steps below to set up the Postman.
Click on the new connection and select WebSocket Request. Select the connection type, here it is Socket.IO and enter your server URL, mine is HTTP://localhost:3000. Now start the server and click on connect, you will see the message saying connected to the server.
Now, enter the event name you want to emit, that you will be emitting from the client side. We have created the event i.e. addition, so let’s write addition in the event name and if there is any callback function meaning, if you will be receiving any response from the event then select the Ack checkbox. In our case, we are receiving a sum of the arguments we are sending in acknowledgement, so I have selected the Ack.
You can also pass arguments to events. Enter the argument value in the message and select your message type from Text, JSON and Binary. If your event accepts multiple arguments, click on + Arg. In our case, we are passing two numbers to perform addition. And then click on send.
Voila!!!! we received the sum in response.
Conclusion
Testing SocketIO in Postman is as easy as testing any other API.
Thank you for reading❤! Hope you have found this helpful.