How can 1 port listen to both http and websocket

Viewed 340

Hello I have a http server running on port 80. Now as I am new to sockets, I got to know that we can upgrade the connection on the same port and make both work on the same port.

Now I am confused what will happen to the other client.

Consider this: I send a get request to the server that please upgrade my connection from http to websocket using the same TCP/IP. The server responded with 101. It upgraded the connection. So far so good.

Now the port 80 is a websocket port rather than http. So now, socket.io would be maintaining the connection and request and not the app in express();

So when the next client comes to the same port 80 to do something non-socket related or anything, how the http mechanism would work because the connected is already a socket one now.

How it is doing what it is doing here.

1 Answers

Now the port 80 is a websocket port rather than http.

A TCP listener socket is agnostic to the application protocol. It is only about establishing a new TCP connection. There are not even application data transferred on the listener socket.

Using a specific application protocol like HTTP or switching between application protocols like switching from HTTP to WebSocket, only effects the newly established (accepted) connection but not the listener socket. This also means that it is possible to use different application protocols on different TCP connections, even if all of these connections originated on a single TCP listener socket.

Related