balderdashy / balderdashy/sails
io.socket.on('connect', doFoo) cannot be removed with io.socket.off
- Dominant language
- JavaScript
- Stars
- 22.8k
- Forks
- 1.9k
- PR merge metrics
- No merged PRs in 30d
Description
**Sails version**: ^1.1.0
**Node version**: v8.14.0
**NPM version**: 6.4.1
**DB adapter name**: N/A
**DB adapter version**: N/A
**Operating system**: Windows 10
I have been researching on and off for about the last 3 weeks on a problem adding a `"connect"` lisener.
I first started back here - https://gitter.im/balderdashy/sails?at=5c811e128f294b134a02c06f
I am very sure of this problem now:
I have set `io.sails.autoConnect = false`, so I call `io.socket = io.sails.connect();` first, but it doesn't matter, Reproducible code, with or without auto-connect:
```
if (io.sails.autoConnect === false) {
io.socket = io.sails.connect();
}
io.socket.on('connect', function observer() {
console.log('CONNECTED!!! you should never see this fire again as i remove this');
io.socket.off('connect', observer);
setTimeout(disThenRecon, 0);
});
function disThenRecon() {
io.socket.on('disconnect', function resolver() {
io.socket.off('disconnect', resolver);
console.log('ok disconnected, will now reconnect, and you will see the console log from above again');
io.socket.reconnect();
});
io.socket.disconnect();
}
```
You will see `CONNECTED!!! you should never see this fire again as i remove this` fire after reconnect. It should not have due to me doing `io.socket.off('connect', observer);`
This only affects `"connect"` `evName`.
I checked out the `sails.io.js` code - And added `console.log`s. This `io.socket.on('connect'` gets added to `this.eventName.connect` queue - https://github.com/balderdashy/sails.io.js/blob/88b96ea019db4a03a1fb5fff0600dc7fd23f7862/sails.io.js#L1161
Then it gets replayed - https://github.com/balderdashy/sails.io.js/blob/88b96ea019db4a03a1fb5fff0600dc7fd23f7862/sails.io.js#L917
Which puts it onto `_raw.on` - https://github.com/balderdashy/sails.io.js/blob/master/sails.io.js#L1127
Then when I do `sails.io.off('connect', ...)` then it does a `raw.off` because `raw` now exists - https://github.com/balderdashy/sails.io.js/blob/88b96ea019db4a03a1fb5fff0600dc7fd23f7862/sails.io.js#L1181-L1182
However for some reason its not removing it. And consequent `io.socket.on('connect` are not registering.
Contributor guide
Research direction
Start with sails.io.js at the referenced lines around the connect-event queue replay and the raw on/off calls. Reproduce the supplied disconnect and reconnect sequence with autoConnect both enabled and disabled, then trace why off('connect', observer) does not remove the replayed listener. Done means the callback does not fire after reconnect and later connect handlers still register normally.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100