Socket.IO middleware, io.use
Solution 1
io.use()
allows you to specify a function that is called for every new, incoming socket.io connection. It can be used for a wide variety of things such as:
- Logging
- Authentication
- Managing sessions
- Rate limiting
- Connection validation
And so on...
It's purpose is similar to Express middleware (like with app.use()
), but this is for incoming socket.io connections, not incoming the regular http requests that Express manages. If you want middleware to process an incoming http request, use Express middleware with app.use()
. If you want middleware to process an incoming socket.io connection, use socket.io middleware with io.use()
.
Solution 2
According to the official documentation of socket.io V 3.0.4 a middleware function io.use
in socket.io is a function that gets executed for every incoming connection like in Express but the only difference is that socket manages the request wherein express HTTP protocol manages the request.
Why socket middleware
- Middleware functions in socket.io are very useful during authentication of request,
- Additional data like credentials can be put in the socket during the connection.
- Middleware function can be used multiple times where each of them runs the given function in order.
Willem van der Veen
Started programming since 2014 in Java because of interest. Since then I developed a passion with a variety of languages and technologies. I am currently making web applications professionally.
Updated on June 14, 2022Comments
-
Willem van der Veen almost 2 years
Working on a web application which is built upon expressJS and Socket.io. In the following post I saw the usage of middleware syntax which was new to me. Here is an example of the syntax:
const io = require('socket.io')(); io.use(function(socket, next) { // execute some code next(); }) .on('connection', function(socket) { // Connection now authenticated to receive further events socket.on('message', function(message) { io.emit('message', message); }); });
It basically uses middleware functions on the
socket.io
instance. My previous understanding was that middlware can only be used on the express instance (app.use(...)
).Questions:
- Is this syntax just regular middleware which works similarly to
app.use(...)
? - If it is different, what are the differences?
- Is this syntax just regular middleware which works similarly to
-
Willem van der Veen about 6 yearsThanks a lot! Sometimes Socket.io could use some more documentation IMO.
-
Giggioz about 6 years@jfriend00 Is it possibile to transform the incoming data inside the io.use() middleware in such a way that the next middleware get them changed?
-
Giggioz about 6 yearsI mean, something like socket.use((packet, next) => { packet.foo = 'bar'; next(); });
-
buycanna.io about 5 years@giggioz -
io.on('connection', (socket) => { socket.use((packet, next) => { if (packet.doge === true) return next(); next(new Error('Not a doge error')); }); });
-
Julian Knight almost 5 yearsNote that unlike Express middleware, socket middleware is only ever called once - on a new connection. So using it to do authentication is very limited. It can only validate the initial connection not any subsequent communications.
-
TheRealChx101 over 4 years@JulianKnight Naturally, as the connections are long lived.
-
Patrick Desjardins about 2 yearsVersion 4.x has middlewares called on every call.
-
jfriend00 about 2 years@PatrickDesjardins - Can you provide a reference for that? When I look up the 4.x doc for
io.use()
, I see this: Note: this function will be executed only once per connection (even if the connection consists in multiple HTTP requests).