A
A
Andrey Rudakov2022-01-11 14:33:04
Node.js
Andrey Rudakov, 2022-01-11 14:33:04

Socket.io why connects twice?

Good afternoon.

const io = require('socket.io-client')
const socket = io("http://localhost:1900", {
    transports: ["websocket"]
})


This code connects twice to the server, here is the server console:
hello: eYldOgqbjYxHj152AAAA
hello: eYldOgqbjYxHj152AAAA
And with the same ID

Server code:
io.sockets.on('connection', function (socket) {
    console.log('hello: ' + socket.id)
})


Why can this happen?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Zapunny, 2022-01-11
@DEMETRA-WORK

Are you doing it in react or native js?
if react then wrap it in useEffect

function main(){
useEffect(()=>{
    const socket = io("http://localhost:1900", {
         transports: ["websocket"]
    })
},[])
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question