30secondstosam
30secondstosam

Reputation: 4686

cURL request for Socket IO on Node (Express)

We want to be able to test the Socket IO connection on our Node (Express) backend without using the SocketsIOClient (which does work!), using cURL (or another command line tool).

I found the following:

curl --verbose -i -N -H "Upgrade: websocket" -H "Connection: Upgrade" http://<API_URL>:<PORT>

However, this does not work. Even when using http://localhost:3000 and running locally, it did not work. This is the response:

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 3000 (#0)
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.64.1
> Accept: */*
> Upgrade: websocket
> Connection: Upgrade
>
* Empty reply from server
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server
* Closing connection 0

Upvotes: 4

Views: 8560

Answers (1)

Željko Šević
Željko Šević

Reputation: 4384

npx wscat -c ws://localhost:3000/socket.io/\?transport=websocket

This worked for me to establish WebSockets connection

If the last line of output is < 40, "event-name" event with "new message" message can be emitted with the following command

> 42["event-name","new message"]

Upvotes: 8

Related Questions