Express + Socket.io synchronization using JWT. Use sockets in your express routes.
Express + Socket.io synchronization using JWT. Use sockets in your express routes with
req.getSocket()
.
This is more a proof of concept than an actual package at the moment and is not entirely tested for great scaling. Use it at your own risk.
Socket.io version >= 2.0
const express = require('express');
const server = require('http').Server(app);
const io = require('socket.io')(server);
const socketAuth = require('express-socket.io-jwt');
const socketMiddleware = socketAuth.createMiddleware(io, {
// Set a static JWT secret :
secret: 'JWT_SECRET',
});
app.get('/ROUTE', socketMiddleware(), (req, res, next) => {
// const userSocket = req.getSocket() ...
});
server.listen(3000);
const express = require('express');
const server = require('http').Server(app);
const io = require('socket.io')(server);
const socketAuth = require('express-socket.io-jwt');
const socketMiddleware = socketAuth.createMiddleware(io, {
// Set a static JWT secret :
secret: 'JWT_SECRET',
// Optional : extracts the JWT from the socket request
jwtFromRequest: ({ cookie, queryParams }, socket) => {
return cookie.jwt; // For cookie usage
return queryParams['token']; // For LocalStorage usage
},
// Optional :
connectionNameFromRequest: ({ queryParams }) => queryParams.name,
// Optional :
onSocketParseError: (err, socket, next) => {
console.error(err);
return next(new Error('Could not decode socket token'));
},
});
app.get('/ROUTE', socketMiddleware({ required: true, matchSocket: 'ip' }), async (req, res, next) => {
// Extract the associated Socket of the request
const socket = req.getSocket();
const payload = await socket.payload;
socket.emit('hello', `Hit to /ROUTE from ${payload.whateverProperty}`);
// ...
});
server.listen(3000);
const socketAuth = require('express-socket.io-jwt');
socketAuth#createMiddleware(io, opts)
Initializes a new middleware function.
Returns a new middleware function socketMiddleware()
.
Arguments :
io
(required, Socket.io Server
) : the socket.io server. Throws an error if not set.opts
(optional, object
, default {}
) : optional initialization options
secret
(required, String
) : the JWT secret string.jwtFromRequest
(function({ cookie, queryParams }, socket)
) : extracts the JWT from the socket connection's cookies or query parameters.
cookie
(object
) : user's cookies passed in the socket connectionqueryParams
(object
) : query parameters passed in the socket connection initialization stringconnectionNameFromRequest
(function({ cookie, queryParams }, socket)
) : extracts the "connection name" from the socket connection's cookies or query parameters. The connection name is set by the socket.io client and allows the handling of multiple socket.io connections for 1 user with req.getSocket([connectionName])
.
cookie
(object
) : user's cookies passed in the socket connectionqueryParams
(object
) : query parameters passed in the socket connection initialization stringonSocketParseError
(function(err, socket, next)
) : TODO
const socketMiddleware = socketAuth.createMiddleware(/* opts */);
socketMiddleware(opts)
Middleware adding the req.getSocket()
method, allowing the use of the client's socket in express routes.
Returns a new middleware, ready to be used.
Arguments :
opts
(optional, default {}
) : optional initialization options
required
(Boolean
, default true
) : if true
, throws an error if the route is accessed without a corresponding socket connectionmatchSocket
(String|function(req, socket) => Boolean
) : check if a HTTP request comes from the same user browser that holds the socket. By default, returns true
if the req
and socket
IP matches. If required
is set to false
, the middleware will not throw and still call next()
. Allowed values : 'ip'
, 'cookie'
or a function.
req
(express Request
) : user requestsocket
(socket.io Socket
) : user socket.io connectionapp.get('/ROUTE', socketMiddleware(), (req, res, next) => {
// const userSocket = req.getSocket() ...
});
req.getSocket([connectionName])
Gets a single socket associated to the user. The socket is validated through the matchSocket
parameter of socketMiddleware(opts)
.
Returns a socket.io Socket
. If no socket is found and the required
parameter is falsy, returns undefined
.
Arguments :
connectionName
(optional, default undefined
) : get a socket initiated with a connection name and passed to connectionNameFromRequest()
.When scaling the app across multiple Node.js instances, it is required to use sticky sessions for socket.io.
const express = require('express');
const server = require('http').Server(app);
const io = require('socket.io')(server);
const socketAuth = require('express-socket.io-jwt');
// Use Redis PubSub to share events across Node.js processes
const redisAdapter = require('socket.io-redis');
// Use sticky sessions
const sticky = require('sticky-session');
const app = express();
const socketMiddleware = socketAuth.createMiddleware(io);
io.adapter(redisAdapter({ host: 'localhost', port: 6379 }));
app.use(socketMiddleware());
// [ Routes ... ]
sticky.listen(server, 3001);
MIT License
Copyright (c) 2017-2018 Nicolas COUTIN
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.