Repository navigation
What's the difference between .close() and .disconnect() in dgram Web Socket #41815
Description
Activity
- addedquestionIssues asking questions about Node.js.Issues asking questions about Node.js.dgramIssues and PRs related to UDP and the dgram module.Issues and PRs related to UDP and the dgram module.docIssues and PRs related to Node.js documentation.Issues and PRs related to Node.js documentation.
on Feb 1, 2022 You're being nominated as collaborator so I'll give you hints instead of a straight up answer to encourage exploration in the code:
Disconnect:
- You can find the code showing what they do in
dgram.js - You will notice disconnect delegates to the handle's disconnect directly, it in turn calls
uv_udp_connect(handle, nullptr)(you can check the libuv docs for what that does :)) - Then returns and sets the state
Close:
- Pushes a call to close to the queue if there is one (what does the queue do? who calls enqueue in that code?)
- Calls close on the handle (where does close come from?)
- You can find the code showing what they do in
.connect()and.disconnect()were added relatively recently (in 2019) by @santigimeno. You may want to look at that PR for some context in addition to the things @benjamingr points you to.Apologies for the delayed reply. I had a busy week.
Thanks for pointing me in the right direction, I will dig into the code and come up with the difference.
I will work on it this weekend.Reacted by Benjamin GruenbaumHere's what I've gathered:
Disconnect:
It unsets the remote port and address set usingconnect(). That doesn't mean we stop receiving messages on the socket. Before disconnecting, we receive messages only from the remote peer set inconnect(), disconnecting removes the exclusivity.
In the code, we directly usedisconnect()on the handle which callsuv_udp_connectwith a NULL value inudp_wrap.ccthat disconnects the handle.Close:
Shuts down the socket. No more messages.
It completely resets the handle.
In the code, we userecvStop()on the handle which callsuv_udp_recv_stopinudp_wrap.ccthat stops listening for incoming messages.So the difference is that
close()stops new messages and shuts down the socket whiledisconnect()simply undoes the effects ofconnect()and the socket keeps listening for messages.Reacted by Benjamin Gruenbaum, Rich Trott, Iamgabrielsoft, Santiago Gimeno, Katrin Yordanova and neweverThe 'close' event is emitted after a socket is closed with close(). Once triggered, no new 'message' events will be emitted on this socket
A synchronous function that disassociates a connected dgram.Socket from its remote address. Trying to call disconnect() on an unbound or already disconnected socket will result in an
The writeup was gotten from https://nodejs.org/api/dgram.html#event-listening
I think #41815 (comment) is the correct answer. I'm going to close this. Feel free to reopen it if you think there's still something to be solved.
Description of the problem
I am trying to resolve an issue on nodejs/help.
The user reported getting
Error [ERR_SOCKET_DGRAM_IS_CONNECTED]: Already connectedwhen reconnecting after deliberately closing the socket connection.I was able to reproduce the issue using
.close()and I also tried.disconnect(). In both cases, I encountered different errors when trying to reconnect after (deliberately) closing/disconnecting the socket.Here's what I would like to understand:
Based on the explanation I intend to create a PR to fix the issue (if any) and also add more context to descriptions of
.close()and.disconnect()indgramdocumentation