Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server-side namespaced disconnect #795

Closed
DanielBaulig opened this issue Mar 18, 2012 · 11 comments
Closed

Server-side namespaced disconnect #795

DanielBaulig opened this issue Mar 18, 2012 · 11 comments

Comments

@DanielBaulig
Copy link
Contributor

As far as I can tell disconnecting from a namespace is only possible from the client side. It does not seem to be possible to disconnect a client from just a namespace from the server side.

Forced disconnection from the server is an essential feature. Multiplexing over a single connection is hampered a lot if a server can only disconnect a client from all namespaces at once. E.g. a module based on socket.io might disconnect a socket if something goes wrong badly. One couldn't pull in that module and use it on a multiplexed connection in it's own namespace without it interfering with other stuff going on on that connection.

The code for dealing with namespaced disconnects essentially is present. The protocol also specifies it. And it is already possible, from the client side.

What do you guys think?

@rauchg
Copy link
Contributor

rauchg commented Mar 18, 2012

socket.disconnect should only close the transport if no other multiplexed sockets are open I think

@DanielBaulig
Copy link
Contributor Author

I have the following setup:

io.of('/one').on('connection', function (socket) {
  socket.log.debug('socket connected to namespace one', socket.id);
  socket.disconnect();
});

io.of('/two').on('connection', function (socket) {
  socket.log.debug('socket connected to namespace two', socket.id);
});

Note, that I do not disconnect the namespace two namespaced socket. However, on the client both on('disconnect') events will fire.

PS: output of socket.io

info  - booting client
info  - transport end by forced client disconnection
debug - websocket writing 0::
info  - transport end

@rauchg
Copy link
Contributor

rauchg commented Mar 18, 2012

I see. That's definitely a bug!

@DanielBaulig
Copy link
Contributor Author

Actually, from looking at the source code it looks more like a "feature":

https://github.com/LearnBoost/socket.io/blob/master/lib/socket.js#L290
https://github.com/LearnBoost/socket.io/blob/master/lib/transport.js#L202

There doesn't seem to be any code to handle that use-case.

@rauchg
Copy link
Contributor

rauchg commented Mar 18, 2012

Yep, that's definitely something we need to change =] Thanks for bringing this up

@DanielBaulig
Copy link
Contributor Author

I played around a bit and it seems that doing the following will actually perform a graceful namespaced disconnect:

socket.packet({type: 'disconnect'});
socket.namespace.manager.onLeave(socket.id, socket.namespace.name);
socket.$emit('disconnect', 'booted');

It does not take into account what happens, if the namespace was the last namespace the client was connected to though. But as I think about it, I don't think anything special should happen when the client disconnects from the last namespace, since it will still be connected to the global namespace anyway.

Also I thin there is another bug, that is not directly related but I found while doing research on this bug: if a client disconnects from a namespace you would expect it to also leave all rooms associated with that namespace. This is not the case though. I will open up another issue and describe that more in details there.

DanielBaulig pushed a commit to DanielBaulig/socket.io that referenced this issue Mar 19, 2012
DanielBaulig pushed a commit to DanielBaulig/socket.io that referenced this issue Mar 19, 2012
@DanielBaulig
Copy link
Contributor Author

The second commit fixes the problem.

lautis added a commit to lautis/socket.io that referenced this issue Apr 6, 2012
…owdock

* refs/remotes/learnboost/master:
  Release 0.9.5
  Added test for polling with connection close.
  Ensure close upon request close.
  Fix disconnection reason being lost for polling transports.
  Ensure that polling transports work with Connection: close
  Log disconnection reason
  Release 0.9.4
  Release 0.9.4
  Release 0.9.4
  Release 0.9.3
  Firefox will try to parse the response from POST requests, causing a syntax error message in the Web Console. Basically an addition to socketio#501
  Fix issue socketio#795
  Add disconnect from namespace test-case for issue socketio#795

Conflicts:
	lib/transports/http.js
	package.json
@nh2
Copy link

nh2 commented Jun 16, 2012

Can this be closed?

@broject
Copy link

broject commented Nov 26, 2019

Yep, that's definitely something we need to change =] Thanks for bringing this up

why do not fix this problems "client namespace disconnect". i never call disconnect event normal process. but call disconnect it is very trouble to all developers.!!

@broject
Copy link

broject commented Nov 26, 2019

WHY This issue was closed.
WHERE IS SOLUTIONS ?????????????????????????????????

@laneschmidt
Copy link

this is still happening immediately after connecting.

here's my implementation:

// server.js
// version: "socket.io": "^2.3.0",

const express = require('express');
const http = require('http');
const cors = require('cors');
const socketIO = require('socket.io');

import { generateToken } from '../utils/helpers');

const port = 80;
const app = express().use(cors);
const server = http.Server(app);
const io = socketIO(server);

io.on('connection', (socket) => {
  console.log('Socket connected');

  const token = await generateToken();

  socket.on('ping', () => {
    io.emit('ping', 'pong');
  });

  socket.on('disconnect', (reason) => {
    console.log('Socket disconnected. Reason:', reason);
  });
});

server.listen(port, () => {
  console.log(`Server started on port ${port}`);
});
// AppComponent.tsx
// version: "socket.io-client": "^2.3.0",

import io from 'socket.io-client';

...

const socket = io(socketURL);
socket.emit('ping');
socket.on('ping', (res: any) => console.log('res', res));

i have verbose debugging enabled but can't see any errors in the console, so i can see that it connects then immediately disconnects.

the frontend logs:

browser.js:133 socket.io-client:socket server disconnect (/) +2ms
browser.js:133 socket.io-client:manager disconnect +17ms
engine.io-client:socket socket close with reason: "forced close" +2ms

the server logs: Socket disconnected. Reason: server namespace disconnect.

nothing is telling to to disconnect.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants