question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Middleware(.use) does not emit connect when using a custom namespace

See original GitHub issue

You want to:

  • report a bug
  • request a feature

Current behaviour

When using a custom namespace with a middleware the server gets a ‘connection’ event, but the client does not receive ‘connect’ event.

This works:

io.use((socket, next) => {
   console.log('middleware running...');
   next();
}).on('connection', socket => {
  console.log('client connected');
});

This does not work:

// Following works, but client does not receive 'connect' or 'connection':
io.of('/admin').use((socket, next) => {
   console.log('middleware running...');
   next();
}).on('connection', socket => {
  console.log('client connected');
});

Sitenotes

Also very strange: If i do the following code then also the default-namespace does not emit any ‘connect’ event even tho the middleware from /admin is not running.

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

io.of('/admin').use((socket, next) => {
   console.log('middleware running...');
   next();
}).on('connection', socket => {
  console.log('client connected');
});

To fix this i have to add .use((socket, next) => { next(); }) to default namespace. But still /admin does not emit ‘connect’.

Steps to reproduce (if the current behaviour is a bug)

Please see above

Expected behaviour

On connection, client should receive ‘connect’

Setup

  • OS: macOS Sierra 10.12.6
  • browser: Chrome
  • Node: v8.2.1
  • NPM: 5.4.2
  • socket.io version: 2.0.3

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
darrachequesnecommented, Mar 10, 2018

@JanisRubens Thanks for the detailed report 👍 , I was eventually able to reproduce. Fixed by https://github.com/socketio/socket.io/pull/3197

2reactions
sittingboolcommented, Apr 26, 2021

I am having the opposite effect: I can only see the logs for the namespaces. But I also connect the client to that namespaces. However I was expecting to be able to use a global middleware for auth.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Socket.io custom namespace clients do not receive an error ...
I am developing a project with socket.io communication. The basic structure is that I have set up a custom namespace which requires ...
Read more >
Middlewares | Socket.IO
A middleware function is a function that gets executed for every incoming connection.
Read more >
Migrating from 2.x to 3.0 | Socket.IO 中文文档
io.set() is removed; No more implicit connection to the default namespace ... Socket.use() is removed; A middleware error will now emit an Error...
Read more >
python-socketio Documentation - manpages.ubuntu!
emit ('my message', {'foo': 'bar'}, namespace='/chat') If the namespaces argument of the connect() call isn't given, any namespaces used in event handlers are...
Read more >
API Reference — python-socketio documentation
Emit a custom event to a client and wait for the response. ... IO transport is connected, and the namespaces will connect in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found