Native Coder Native Coder - 4 days ago 5
Node.js Question

How can I configure Ubuntu Server to use Node.js for WebSockets, and Apache2 for everything else?

I have a very simple WebSocket Server built with Node.js and the WS package.

var WebSocketServer = require('ws').Server
var wss = new WebSocketServer({
port: 8080
});

wss.on('connection', function(client) {

client.on('message', function(message) {
console.log('received: %s', message);
});

client.send('something');
});


// Console will print the message
console.log('-=-=- Server running :8080 -=-=-');


When I try to connect using a dead-simple client, the connection times out.

<script>
var ws = new WebSocket('ws://192.168.1.75:8080');

ws.onconnect = function(){
console.log("Connected!");
};


</script>


The server starts/runs without error, but the client always times out the connection. Do I need to configure iptables or apache to send the traffic on port 8080 to my node server that I wrote? I was thinking that the server would automatically start listening on port 8080. (I apologize in advance if this is an amature-ish question. I'm VERY new to Node.js)

Answer

I don't think you have to do anything special regarding Apache so long as you don't have any Apache traffic serving on the same port. You will just have two separate servers running at the same time on separate ports and will have to direct your traffic accordingly.

If your server is working but it's timing out when you exit your ssh connection it's probably because the server connection terminates when your ssh connection does.

Try running the process in the background by starting the server using the command:

nohup node server.js > /dev/null 2>&1 &
Comments