javascripttestingwebsocketpostman

testing websockets with postman new api


I am trying to get the postman api for websockets working, however I can't even make a connection:

When I try to make a connection

ws://localhost:5001/socket.io/

results in:

Error: Unexpected server response: 400 //for ws

//index.js
const port=5001
io.on("connection", (socket) => {
  //handshake and hydrate and test
  console.log("Client connected",socket.id);
  socket.on("test", (data)=>{
    console.log("test data is:",data)
    socket.emit("test", "server heard you!")
  });

what is the proper url string to connect to a socket.io/express/nodejs server?


Solution

  • Socket.io supports polling and websocket transports.

    To connect with Postman's WebSocket client, we have to explicitly set the WebSocket transport while connecting to a Socket.io server. This can be done by setting the transport=websocket parameter in the connection URL.

    In your case, it will be: ws://localhost:5001/socket.io/?transport=websocket.