Page 2

SOCKET.IO SERVER TESTING

How to test your Socket.IO server using Postman

Has the thought of using postman to test your socket.io server ever crossed your mind? The latest version of postman supports socket.io testing with some limitations which are listed below. Moreover, I have also demonstrated an example of how message passing works between the socket.io server and postman (client).

Currently supported features on Postman ✅

  • Creating Socket.IO requests

  • Passing in the socket.io address and request body/payload

  • Supports text , json and binary formats for request body

Currently unavailable features on Postman ❌

  • API testing not supported

  • On socket.io server reconnection, message history is lost

  • Web socket traffic is not tracked in postman console

  • Unable to save socket.io request in postman collection

Connecting to a socket.io server

Below is a code snippet of how to initialise/init the server by attaching it to an existing http server in nodejs.

var server = http.createServer(app);const socketio = require('socket.io');var io = socketio(server);

Socket.io server events

Currently we have an event joinRoom which will take in input from the client and emit an event roomUsers and roomSettings to the socket.io server.

io.on('connection',(socket)=>{    console.log('socket is ready for connection');    socket.on('joinRoom', ({ ...roomObject }) => {        const user = userJoin(socket.id, roomObject.user.name, roomObject.room_uuid,roomObject.user.user_uuid);        socket.join(user.room);        socket.emit('message', 'Welcome to application'+user.username);        socket.broadcast            .to(user.room)            .emit(                'message',               `${user.username} has joined the call`            );        io.to(user.room).emit('roomUsers', {            room: user.room,            users: getRoomUsers(user.room)        });        io.to(user.room).emit('roomSettings', {            ...roomObject        });    })

Socket.IO requests in Postman

A new Socket.IO request cannot be created by opening a new tab hence we need to go to New > WebSocket Request.

Select Socket.IO from the dropdown and key in the HTTP server url in the address bar.

We can start sending and receiving message once the connection is established.

In the screenshot 2.1 above you can see that I have added the joinRoom and roomUsers events which I have defined in my socket.io server code. I have used postman as a socket.io client to send an event joinRoom with a JSON input to the server.

In the messages section (refer to image 2.2), postman logs the joinRoom event along with the roomUsers event which the server should receive. The roomUsers event returns to us the number of users connected to that socket. Postman also supports searching and deletion of message logs.

Finally

Hope the above mentioned instructions will help you to send socket.io requests with Postman and provide a beginners insight to this new feature of Postman which is still in beta stage. Would love your feedback!

Last updated