FAQ

Can I use wildcards in events?

Not in Socket.IO directly, but check out this plugin by Hao-kang Den. It provides a Socket.IO middleware to deal with wildcards.

Prevent flooding from single connection?

Limit number of events by IP, uniqueUserId or/and socket.id with rate-limiter-flexible package.

Socket.IO with Apache Cordova?

Take a look at this tutorial.

Socket.IO on iOS?

Take a look at socket.io-client-swift.

Socket.IO on Android?

Take a look at socket.io-client.java.

Usage with express-session

  1. const express = require('express');
  2. const session = require('express-session');
  3. const app = express();
  4. const server = require('http').createServer(app);
  5. const io = require('socket.io')(server);
  6. const sessionMiddleware = session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }});
  7. // register middleware in Express
  8. app.use(sessionMiddleware);
  9. // register middleware in Socket.IO
  10. io.use((socket, next) => {
  11. sessionMiddleware(socket.request, {}, next);
  12. // sessionMiddleware(socket.request, socket.request.res, next); will not work with websocket-only
  13. // connections, as 'socket.request.res' will be undefined in that case
  14. });
  15. io.on('connection', (socket) => {
  16. const session = socket.request.session;
  17. session.connections++;
  18. session.save();
  19. });
  20. const port = process.env.PORT || 3000;
  21. server.listen(port, () => console.log('server listening on port ' + port));