socket io creating multiple connection on reload

socket io creating multiple connection on reload



I am trying to make a socket io push logs to client UI.
Here is the code in angular 2


ngAfterViewInit()
this.socket=io('http://localhost:9999')
this.socket.on('send-log-data-'+this.labName,function(data)
this.loaddata(data)
.bind(this))

@HostListener('window:beforeunload', ['$event'])
doSomething($event)
// if(this.hasChanges) $event.returnValue='Your data will be lost!';
this.socket.emit('disconnect',"")

}



After looking in to server side i can see on every reload of web page two new connection are getting created and one connection is getting closed.



On First Reload


new connection made
new connection made
Reloading
Reloading
disonnect request came.Disconnecting



On second reload


Reloading
new connection made
new connection made
new connection made
new connection made
Reloading
Reloading
disonnect request came.Disconnecting
disonnect request came.Disconnecting



Server side code as asked is


var Tail=require('tail').Tail
module.exports =

io_connect_disconnect : (labName,io) =>

io.on('connection',function(socket)
console.log("new connection made")
filename="output.log"
var options= fromBeginning: true
tail = new Tail(filename,options)

console.log("new connection made")
tail.on("line", function(data)
console.log("triggering")
console.log(data)
io.sockets.emit('send-log-data-'+labName,data)
)
socket.on('disconnect',function()
tail.unwatch()
console.log("disonnect request came.Disconnecting")
socket.disconnect();
)
)




Can anyone help me with this strange situation ?






Can you share your server side code?

– Gili Yaniv
Oct 19 '17 at 6:37






@GiliYaniv I have edited the question with server-side code.Please check

– ivan R
Oct 19 '17 at 9:09






From where the 'reloading' comes from?

– Gili Yaniv
Oct 22 '17 at 6:29






@GiliYaniv Reloading is coming when it is actually redirecting to required page

– ivan R
Oct 22 '17 at 14:48






Not sure, But I'm guessing that because you're biding "this" to your socket.io instance and not seems to dismiss in your 'onDestroy' implementation your first connection isn't really terminate. It's nice that you're sending 'disconnect' emit but it doesn't actually dismiss it. You should dismiss you're socket instance by this.socket.disconnect(). Try this and see if it still happens..

– Gili Yaniv
Oct 23 '17 at 6:29





1 Answer
1



The problem is that you are registering a new event handler every time the page is reloaded. This causes the data to be sent multiple times. I had the same issue and I solved by using io.once() instead of io.on() in the server.


io.once()


io.on()


io.once('connection',function(socket)
console.log("new connection made")
...



This causes the connection listener to be unregistered as soon as it is called, and the data is sent only once.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)