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

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

Closed
1 of 2 tasks
MickL opened this issue Oct 11, 2017 · 13 comments
Closed
1 of 2 tasks

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

MickL opened this issue Oct 11, 2017 · 13 comments
Labels
bug Something isn't working
Milestone

Comments

@MickL
Copy link

MickL commented Oct 11, 2017

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
@darrachequesne
Copy link
Member

Hi! How are you initializing the client? The following seems to work for me:

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

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

// client
const socket = require('socket.io-client')('http://localhost:3000/admin');

socket.on('connect', onConnect);

function onConnect(){
  console.log('connect ' + socket.id);
}

Output (fiddle):

server listening on port 3000
(default) middleware running...
(default) client connected
(admin) middleware running...
(admin) client connected

@MickL
Copy link
Author

MickL commented Oct 19, 2017

No it is not working for me. Full code, working example:

Server:

let express = require('express'),
    app = new express(),
    server = app.listen(3005, 'localhost'),
    io = require('socket.io')(server)
;

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

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

Client:

$(function () {
    let socket;

    $('#btn-connect').click(() => {
        console.log('Connecting ...');

        socket = io('http://localhost:3005');

        socket.on('connect', () => {
            console.log(`Connected to ${url} with id: ${socket.id}`);
        });

        socket.on('disconnect', reason => {
            console.log(`Disconnected. Reason: ${reason}`);
        });
    });

    $('#btn-connect-admin').click(() => {
        console.log('Connecting to admin ...');

        socket = io('http://localhost:3005/admin');

        socket.on('connect', () => {
            console.log(`Connected to ${url} with id: ${socket.id}`);
        });

        socket.on('disconnect', reason => {
            console.log(`Disconnected. Reason: ${reason}`);
        });
    });
});

If i remove the middleware i can connect to both. If i add the middleware to admin only, i get no connect event on default namespace nor /admin.

Server shows everything correctly:

  • client connected
  • middleware running...
  • admin connected

@MickL
Copy link
Author

MickL commented Nov 16, 2017

Nothing? :(

@JanisRubens
Copy link

JanisRubens commented Jan 16, 2018

@MickL Any updates on this?
Ran into the same issue as well.
Not sure if you spotted this. The only connection issue is with root namespace and if you add a middleware also for the root path, it will be able to connect to it as well. ( this can be a quick fix tbh )

It comes down to following scenarios:

If namespace A('/)' and B('/otherPath') has no middleware it connects to both fine;
If namespace A has no middleware and B has. It connects to A on server side, but client sides 'connect' event listener never gets called, B namespace connects fine;
If namespace A and B both has middleware it can connect to both;

@MickL
Copy link
Author

MickL commented Jan 16, 2018

No i used a workaround. Instead of:

io.of('/admin')
  .use((socket, next) => {
    if(tokenValid()) {
       next();
    } else {
      next(new Error('Authentication error'));
    }
  })
  .on('connection', socket => {
    console.log('admin connected');
  })
;

I wrote:

io.of('/admin')
  .on('connection', socket => {
    if(!tokenValid()) {
       socket.disconnect();
    }

    console.log('admin connected');
  })
;

I spent a lot on this and it seems like the client does connect to the server but the client does not get the connection event.

@JanisRubens
Copy link

I guess this can work. But it's a shame that we can't use middleware properly for its intended use.

@JanisRubens
Copy link

JanisRubens commented Jan 16, 2018

@darrachequesne The example you provided and tested on is not as per reported issue.

You need to remove the middleware from the default namespace and then try to connect.

Like this:

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

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

// client
const socket = require('socket.io-client')('http://localhost:3000/');
const socket_two = require('socket.io-client')('http://localhost:3000/admin');
//wont trigger
socket.on('connect', onConnect);
//will trigger
socket_two.on('connect', onConnect);

function onConnect(){
  console.log('connect ' + socket.id);
}

Now it should fail.
See the cases when it works/does not work above.

Cheers.

@darrachequesne darrachequesne added the bug Something isn't working label Mar 6, 2018
@darrachequesne
Copy link
Member

@JanisRubens Thanks for the detailed report 👍 , I was eventually able to reproduce. Fixed by #3197

@darrachequesne darrachequesne added this to the 2.1.0 milestone Mar 10, 2018
@adeelhussain
Copy link

I am still facing this!
Before creating namespace client receive events but after adding namespace and using middleware, client is not receiving any event however the frames was showing on the chrome.

@ElioTohm
Copy link

+1

@darrachequesne
Copy link
Member

@adeelhussain @ElioTohm which version are you using? Are you able to reproduce the case with the fiddle?

@ElioTohm
Copy link

using socketio v2.1.1 and socket.io-redis v5.2.0
i will try to reproduce it with fiddle asap

@sittingbool
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants