Socket.IO middleware, io.use

前端 未结 2 865
情歌与酒
情歌与酒 2021-02-08 13:33

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 t

2条回答
  •  误落风尘
    2021-02-08 13:53

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

提交回复
热议问题