Socket.IO middleware, io.use

15,294

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:

  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().

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.
Share:
15,294
Willem van der Veen
Author by

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, 2022

Comments

  • Willem van der Veen
    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:

    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
    Willem van der Veen about 6 years
    Thanks a lot! Sometimes Socket.io could use some more documentation IMO.
  • Giggioz
    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
    Giggioz about 6 years
    I mean, something like socket.use((packet, next) => { packet.foo = 'bar'; next(); });
  • buycanna.io
    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
    Julian Knight almost 5 years
    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.
  • TheRealChx101
    TheRealChx101 over 4 years
    @JulianKnight Naturally, as the connections are long lived.
  • Patrick Desjardins
    Patrick Desjardins about 2 years
    Version 4.x has middlewares called on every call.
  • jfriend00
    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).