primus: Middleware example is outdated, can't get it working anymore
Version: 6.0.8
Environment:
- Operating system: Windows 10
- Browser: Google Chrome
- Node.js: 7.3.0
- Transformer: uws
Expected result: Establish a connection between server and client Able to set and read session object shared between Express and Primus
Actual result: If I set
primusSession.upgrade = true // or not set it at all
I can read and set the session object at spark.request.session as expected. However, I get the following error in my browser when trying to connect
WebSocket connection to 'ws://localhost:3000/gameserver?_primuscb=LcrjIwC' failed: Connection closed before receiving a handshake response
opening @ primus.js:3212
emit @ primus.js:300
reconnect @ primus.js:3246
emit @ primus.js:279
reconnect @ primus.js:2341
emitter @ primus.js:146
emit @ primus.js:1007
delay @ primus.js:849
tickedtock @ primus.js:1316
If I set
primusSession.upgrade = false
The browser connects to the client successfullly however the middleware doesn’t get called at all, and I can’t set or read sessions in Primus.
Steps to reproduce:
I’ll try to set up a clean project to reproduce the issue, but here are code snippets for now.
primus-session.js
/**
* Slightly modified version of this middleware example
* Notably, all references to cookie-parser have been rewritten since the latest express-session module doesn't use it anymore
* https://github.com/primus/primus/blob/master/examples/middleware/session.js
*/
'use strict'
//
// Expose the configuration function.
//
module.exports = function configure(options) {
const store = options.store
const primus = this
if (!store) {
//
// Throw an error when the session store is not passed.
//
const message = 'Session middleware configuration failed due to missing '
+ '`store` option'
throw new Error(message)
}
//
// The actual session middleware. This middleware is async so we need 3
// arguments.
//
function session(req, res, next) {
//
// The session id is stored in the cookie
//
const sid = req.headers.cookie
console.log('sid:', sid)
//
// Default to an empty session.
//
req.session = {}
//
// If we don't have a session id we are done.
//
if (!sid) return next()
//
// Grab the session from the store.
//
store.get(sid, function (err, session) {
//
// We don't want to kill the connection when we get an error from the
// session store so we just log the error.
//
if (err) {
primus.emit('log', 'error', err)
return next()
}
if (session) req.session = session
next()
})
}
// Don't call this middleware in an Upgrade request
session.upgrade = false
session.http = true
return session
}
Express configuration
// Sessions are shared between Express and Primus
const store = new expressSession.MemoryStore()
const session = expressSession({
secret: 'change in production',
resave: true,
saveUninitialized: true,
store,
})
// Express settings and middleware
app.set('port', process.env.PORT || 3000)
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
app.use(logger('dev'))
app.use(express.static(path.join(__dirname, 'public')))
app.use(session)
Primus configuration
const primus = new Primus(server, {
pathname: '/gameserver',
transformer: 'uws',
})
// Primus middleware
primus.use('session', primusSession, { store })
primus.use((req, res) => {
console.log(req.session)
})
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 19 (11 by maintainers)
I meant that the middleware example in
/examplesruns without any modification (usingwebsocketsas default transformer).Hmm, that’s surprising. Feel free to close the issue, I’ll create another one at
uws. Thanks for all the help!@prashcr sockjs is “special” as it overrides the
sessionproperty. If you change the Primus example like this:it works and that’s another reason why
expression-sessionshouldn’t be used directly.So the Primus example works for you on Windows when using uws?