altruios
altruios

Reputation: 1083

postman websocket api, "connected" - but no messages or acknowledgement from server

I can not get postman to connect to the server using websockets.

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

in postman the request address is:

ws://localhost:5001/socket.io/?transport=websocket

the symptoms are: postman says it's connected. but if I try to send anything - it disconnects after a timeout. if I set the reconnection attempts to 1, it will automatically reconnect when it disconnects...

but I don't think it's actually connecting - because nothing is happening on the server (no new client connected message)

the format of messages I have also experimented with, to no avail.

42["test","i hear you"]
42[test,i hear you]
["test":"i hear you"]
{"test":"I hear you"}
42{"test":"I hear you"}
{"event":"test","data":"I hear you"}
42{"event":"test","data":"I hear you"}
42["event","test","data","I hear you"]
["event","test","data","I hear you"]

I have inspected the console results, and have not found leads there yet. what could I be missing?

Upvotes: 3

Views: 8183

Answers (3)

Tom Green
Tom Green

Reputation: 53

Because you don not add listener. Add listener "handshake" to postman. You will receive message. enter image description here

This is my code:

io.on('connection', () => {
    console.log('user connected');
    setInterval(() => {
        io.emit('msg', { data: [1, 2, 3] });
    }, 5000);
});

Upvotes: 1

Udit
Udit

Reputation: 121

Postman v8.10.0 added support for Socket.IO, read more.

Socket.IO in Postman

Just enter ws://localhost:5001 as the connection URL and hit Connect.

Also, you can configure the client version (default: v3), handshake path (default: /socket.io), and other reconnection configurations in the request settings.

Upvotes: 2

Filip Culig
Filip Culig

Reputation: 125

You are using socket.io as WebSocket and that does not work because socket.io is not an implementation of websocket.

From official socket.io documentation:

Socket.IO is NOT a WebSocket implementation. Although Socket.IO indeed uses WebSocket as a transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either.

// WARNING: the client will NOT be able to connect!
const socket = io("ws://echo.websocket.org");

Source: https://socket.io/docs/v4#What-Socket-IO-is-not

Upvotes: 1

Related Questions