0

my nodejs script is:

const http = require("http");
const httpserver = http.createServer();
httpserver.on('connection', socket=>{
    console.log(socket.remotePort, socket.address());
})
httpserver.listen(8080)

when navigating to the url: http://localhost:8080/ in Chrome I see that 2 connections are established on consecutive remote ports ex:

55413 {address: '::1', family: 'IPv6', port: 8080}
55414 {address: '::1', family: 'IPv6', port: 8080}

I'm confused as to why it is establishing 2 connections. Other browsers don't do this. Any thoughts are greatly appreciated. Thanks

  • 1
    maybe one of these answers will help : https://stackoverflow.com/questions/58498400/opening-a-single-chrome-tab-causes-multiple-remote-connections https://stackoverflow.com/questions/4761913/server-socket-receives-2-http-requests-when-i-send-from-chrome-and-receives-one – ddor254 Sep 22 '21 at 10:20
  • It may be the OPTIONS request - https://stackoverflow.com/questions/27915191/how-does-the-chrome-browser-decide-when-to-send-options – Pablo Aragon Sep 22 '21 at 11:52
  • 1
    My feeling is that it may indeed be in anticipation of more requests. Clients will make up 6 connections for parallel requests and responses. – Evert Sep 22 '21 at 13:21

1 Answers1

0

I updated the node code to the following:

const http = require("http");
const httpserver = http.createServer();

httpserver.on('connection', socket=>{
    console.log(socket.remotePort, socket.address());

    socket.on('data',(data)=>{
        console.log(socket.remotePort, socket.bytesRead);
        console.log(data.toString())
    })
    socket.on('end',(data)=>{
        console.log(socket.remotePort, 'ended');
    })
})

httpserver.on('request', (req,res)=>console.log('request is made made'));

httpserver.listen(8080);

after refreshing the page a few times it seems that Chrome establishes 2 socket connections and uses the first to receive the GET request. The second receives no data. Upon refresh of the page it ends the first socket connection ( that was used for the GET request); opens a new socket connection (3rd) then uses the 2nd created for the new GET request. On each consecutive refresh it creates a new socket connection and uses the previous opened one, while ending the socket used for the previous GET request.

So it seems Chrome is creating a second socket in anticipation of a page refresh?