13

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:

  1. Is this syntax just regular middleware which works similarly to app.use(...)?
  2. If it is different, what are the differences?
Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155

2 Answers2

25

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:

  1. Logging
  2. Authentication
  3. Managing sessions
  4. Rate limiting
  5. 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().

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 6
    Thanks a lot! Sometimes Socket.io could use some more documentation IMO. – Willem van der Veen Apr 25 '18 at 07:40
  • @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 May 09 '18 at 10:49
  • 1
    I mean, something like socket.use((packet, next) => { packet.foo = 'bar'; next(); }); – Giggioz May 09 '18 at 10:59
  • @giggioz - ```io.on('connection', (socket) => { socket.use((packet, next) => { if (packet.doge === true) return next(); next(new Error('Not a doge error')); }); });``` – buycanna.io Apr 27 '19 at 11:31
  • 4
    Note 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. – Julian Knight Jun 22 '19 at 19:17
  • @JulianKnight Naturally, as the connections are long lived. – TheRealChx101 Nov 27 '19 at 23:12
  • Version 4.x has middlewares called on every call. – Patrick Desjardins May 06 '22 at 04:42
  • 1
    @PatrickDesjardins - Can you provide a reference for that? When I look up the [4.x doc](https://socket.io/docs/v4/middlewares/) 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).* – jfriend00 May 06 '22 at 04:51
1

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.
fktmjohn1
  • 21
  • 4