I have been having issues for a few days trying to set up a simple websocket server on an Amazon EC2 instance. I am using a micro instance of Linux. After booting up for the first time, I updated all OS packages, followed this great guide for getting node and npm installed, installed socket.io, express (not currently using), and websocket.io which includes the websocket-server by miksago.
First, my attempts with Socket.io as the backend. I set up a simple server using the sample code from their website, listed below. I run this with sudo node server.js
. I use sudo so that I can listen on port 80, errors result otherwise. From here, I put my amazon public DNS into the location field at the websocket.org echo test. When I press connect, my server will output debug - destroying non-socket.io upgrade
. I have a console output within the "connection" listener that never gets printed, so it seems that there is an error before the connection request gets through. It may be that I have to use socket.io client side for a socket.io server to work, but I could not find any definitive answer on that.
My second attempt was using websocket-server by Miksago. I'll list the source for the server below as well. This time, I was able to see that console output on the connection request, but a connection was still not established. I tried printing the connection.header object but received an error.
I'm trying to set up a simple server just to get the basics going and I haven't been able to pin down what the issue is. I'm using websocket.org to test the server since their client is set up correctly. What am I doing wrong setting up the server?
Socket.io server
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
console.log("Connection request on socket.io server");
});
Websocket-Server
var ws = require("websocket-server");
var server = ws.createServer();
server.addListener("connection", function(connection){
console.log("Connection request on Websocket-Server");
});
server.listen(80);
Edit: Debug output from node-websocket-server. This is a snippet of the output from console.log(connection._req);
The information looks right, but the server is simply not establishing the connection.
httpVersion: '1.1',
complete: true,
headers:
{ upgrade: 'websocket',
connection: 'Upgrade',
host: 'ec2-50-16-107-223.compute-1.amazonaws.com',
origin: 'http://websocket.org',
'sec-websocket-key': 'HdgJaiL2TZ4iyOHTarmlig==',
'sec-websocket-version': '13' },
trailers: {},
readable: true,
url: '/',
method: 'GET',
statusCode: null,
Edit2: Use websocket.io as a websocket server on Node.js. It works without needing client side js files, and is more up to date with spec. It is available through npm and git.