I'm transitioning a pre-existing project from Socket.io (socket-io / socketio) to plain Websockets and I'd like to run both side-by-side during the transition.
What I'd Expect To Do
'use strict';
var app = require('express')();
var server = require('http').createServer(app);
var ws = require('ws');
var wss = new ws.Server({ noServer: true })
var io = require('socket.io')({ path: '/api/socket.io' });
server.on('upgrade', function upgrade(req, socket, head) {
if ('/api/ws' === req.url) {
wss.handleUpgrade(req, socket, head, function (ws) {
wss.emit('connection', ws, req);
});
} else if ('/api/socket.io' === req.url) {
io.XXXXX() // ??? <=== MISSING DOCUMENTATION FOR THIS
} else {
socket.destroy();
}
});
// ... handle connections, listen on port 3000, etc
Previous Solutions
ws
has excellent documentation showing exactly how to do this in the standards-based way using the upgrade
header:
I've also found a post about how to do it in a way that is proprietary to Socket.io:
And I even found a old reference to how to co-mingle the two with a very, very old version of Socket.io:
But I'm not clear on how to mix Socket.io v2s proprietary / private internal implementation way of doing it with ws
's standard header upgrade technique.
Is it possible to just use the old-fashioned upgrade
event? If not, what's the new strategy for handling the competition between the two?
It seems that I'll be able to figure out how to handle successful requests to each, but the most puzzling part is how to reject requests that are handled by neither.