Socket.IO middleware, io.use

前端 未结 2 875
情歌与酒
情歌与酒 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:55

    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.

提交回复
热议问题