Reputation: 163549
In an effort to be able to send binary data while utilizing Socket.IO's RPC functionality, I thought that I could use both Socket.IO and the WS module on the same server. Rather than opening up completely separate servers to make both connections, I am wondering if I can utilize the same HTTP server.
Is it possible to use only one server created with http.createServer()
for both Socket.IO and WS at the same time? To be clear, I anticipate creating both a Socket.IO connection and a regular WebSocket connection from the client. The following code creates protocol errors on the client side, presumably because both Socket.IO and WS are attempting to handle the connection.
var http = require('http');
var server = http.createServer(app);
server.listen(3000);
// Socket.IO
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
// ...
}
// ws
var WebSocketServer = require('ws').Server
var wss = new WebSocketServer({server: server});
wss.on('connection', function (ws) {
// ...
}
Upvotes: 5
Views: 3646
Reputation: 2412
As of 2016 I simply could assign the websocket module ws a path
var wss = new WebSocketServer({ server: server, path: '/ws' }); //do not interfere with socket.io
No need to alter the socket.io side at all
Upvotes: 3
Reputation: 163549
It turns out that this is possible with some configuration. The trick is to tell Socket.IO not to destroy non-Socket.IO WebSocket connection requests, and then to put Socket.IO and WS on separate paths. Here is some messy example code, but it works while reusing the Socket.IO session ID for the secondary connection.
var server = http.createServer(app);
server.listen(3000);
var WebSocketServer = require('ws').Server
var io = require('socket.io').listen(server);
io.set('destroy upgrade', false);
io.set('transports', ['websocket']);
io.sockets.on('connection', function (socket) {
var wss = new WebSocketServer({
server: server,
path: '/anythingYouWant/' + socket.id
});
wss.on('connection', function(ws) {
ws.on('message', function(message) {
console.log(message);
});
});
});
Upvotes: 5