Skip to content

use() middleware is not triggered #4237

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

Closed
SStoliarchuk opened this issue Jan 4, 2022 · 4 comments
Closed

use() middleware is not triggered #4237

SStoliarchuk opened this issue Jan 4, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@SStoliarchuk
Copy link

SStoliarchuk commented Jan 4, 2022

Describe the bug
of(/./).use() middleware is not triggered when using eiows.Server apparently also if we dont use the eiows.Server module it still shows the same behavior

To Reproduce
We need three files, 1 server 2 client.
Launch the server with nodemon server.js and open localhost:5656/1 and localhost:5656/2
Now both tabs should be connected to the socket io instance and in the terminal where the server was launched there would be printed

/admins slug __next__
/clients slug __next__

Put the terminal with nodemon on one side and the localhost:5656/2 tab on the other side with the devtools console open
Save the file server.js with no changes and nodemon should restart the server, now in the console you will see only

/admins slug __next__

Now even if you reload the localhost:5656/2 page the console.log would still not work.

NOTE: Sometime it may work, so try to save again it takes some tries. I found that the best way to reproduce is to look at the devtools console and spam ctrl+s on the server.js file so that nodemon keeps restarting the server until the localhost:5656/2 devtools console throws an error saying ws connection cannot be made. When you see the error let the server start and the error should appear

Socket.IO server version: 4.4.0
Server

const express = require('express');
const http = require('http');
const { Server } = require("socket.io");
const eiows = require('eiows');

const app = express();
const server = http.createServer(app);

app.get('/1', (req, res) => {
  res.sendFile(__dirname + '/broken_socket.html');
});
app.get('/2', (req, res) => {
  res.sendFile(__dirname + '/broken_socket2.html');
});

const io = new Server(server, {
	path: '/socket',
	serveClient: false,
	wsEngine: eiows.Server,
});

io.of(/./).use(async (socket, next) => {
	socket.join(socket.handshake.auth.slug);
	io.of('/clients').to(socket.handshake.auth.slug).emit('6', {namespace: socket.nsp.name});
	console.log(socket.nsp.name, socket.handshake.auth.slug, '__next__');
	next();
});

server.listen(5656, () => {
  console.log('listening on *:5656');
});

Socket.IO client version: 4.4.0

Client 1

<script src="https://cdn.socket.io/4.4.0/socket.io.min.js" integrity="sha384-1fOn6VtTq3PWwfsOrk45LnYcGosJwzMHv+Xh/Jx5303FVOXzEnw0EpLv30mtjmlj" crossorigin="anonymous"></script>
<script>
	io('http://127.0.0.1:5656' + '/admins', {
		path: '/socket',
		transports: ['websocket'],
		auth: { slug: 'slug', token: 'token' },
	});
</script>

Client 2

<script src="https://cdn.socket.io/4.4.0/socket.io.min.js" integrity="sha384-1fOn6VtTq3PWwfsOrk45LnYcGosJwzMHv+Xh/Jx5303FVOXzEnw0EpLv30mtjmlj" crossorigin="anonymous"></script>
<script>
	io('http://127.0.0.1:5656' + '/clients', {
		path: '/socket',
		transports: ['websocket'],
		auth: { slug: 'slug', token: 'token' },
	});
</script>

Expected behavior
The use() callback that contains the console.log should always be triggered but it is not

Additional context
Please note that the error is caused by this line during the middleware:
io.of('/clients').to(socket.handshake.auth.slug).emit('6', {namespace: socket.nsp.name});
we target /clients in this call, the same namespace that is not logged in the console.log

@SStoliarchuk SStoliarchuk added the bug Something isn't working label Jan 4, 2022
@SStoliarchuk SStoliarchuk changed the title use() middleware is not triggered when using eiows.Server use() middleware ~is not triggered when using eiows.Server~ Jan 4, 2022
@SStoliarchuk SStoliarchuk changed the title use() middleware ~is not triggered when using eiows.Server~ use() middleware is not triggered Jan 4, 2022
@SStoliarchuk
Copy link
Author

SStoliarchuk commented Jan 4, 2022

I think this relates to:
#3960
#4015
#4164

The current workaround that I have found it to emit on a setTimeout() with a 1 sec delay, less than that sometimes still breaks

After reading better those issues I understand that this behavior is caused by the fact that when /admins connects first, it outputs to /clients which creates a namespace that has priority over the regex /./ match.

Would it be possible to still trigger the regex match? I think that would solve these 4 tickets

In the meanwhile the solution is to manually declare each namespace

@yalcinozer
Copy link

Hi. I'm adding a middleware on default the namespace. Unfortunately, after adding middleware, io instance stops firing events. Is this the same issue or unrelated? If I remove "use", everything works as expected.

Output:

io.of("/").use((socket, next) => {
  try {
    let data = verifyUserToken(socket);
    socket.data = data;
    next();
  } catch (error) {
    next(new Error("Socket middleware error"));
  }
});

@darrachequesne
Copy link
Member

@yalcinozer I don't think this is related. Could you add a console.log() in the catch (error) {} block, to see if anything has gone wrong?

@darrachequesne
Copy link
Member

darrachequesne commented Jun 19, 2023

Closed due to inactivity, please reopen if needed.

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

3 participants