Trouble Understanding how upgrading a request works for Web Sockets

15 Views Asked by At

So I'm learning Web Sockets right now to see how I can create real time communication between client and server and just ran into a roadblock. According to the docs in order to create a Web Socket Connection you need to send a regular HTTP Request with a upgrade header. I didn't pay much attention to this as I would just do

const socket = new WebSocket('ws://localhost:3000');

And I would be good to go. I would see from my server that everything is working fine.

const server = app.listen(port);
const wss = new WebSocketServer({server});
wss.on('connection', (connection, req) => {
    console.log('The handshake was successful. The regular HTTP Request with the Upgrade header has been converted to a Web Socket connection.');
});

But going back to what the docs said. You can just send a regular HTTP Request with the Upgrade header and it will turn into a Web Socket connection as its changing protocols. So I tried doing that like so.

enter image description here

But its not working. At all. How can I do this? Without using the WebSocket API provided by JavaScript?

0

There are 0 best solutions below