P.Yntema
P.Yntema

Reputation: 607

Socket.io listening on port not working

So I have a very basic script which should only output a status code 200 when I visit my website on port 8081. But it doesn't seem to be working. Anybody knows why this is the case? I think it maybe has to do with ssl I am using, or am I forgetting something else?

//Begin config
var http = require('http');
var io = require('socket.io')(http);

http.createServer(function (request, response) {
    // Send the HTTP header
    // HTTP Status: 200 : OK
    // Content Type: text/plain
    response.writeHead(200, {'Content-Type': 'text/plain'});

    // Send the response body as "Hello World"
    response.end('Hello World\n');
}).listen(8081);

I copied this from the internet to simply test if socket.io works correctly. I'm trying this on this website. Thanks in advance!

Edit: I did run it using node script.js.

Upvotes: 0

Views: 151

Answers (1)

Tamas Szoke
Tamas Szoke

Reputation: 5542

Use the http server as a variable and put the socket.io line to the end, change port:

var server = http.createServer(/*...your code...*/).listen(3000);
var io = require('socket.io')(server);

Update

I think this could be a port error, try to open the required port or check if something blocks it.

I made a little app to test Socket.IO, you can see the connection changes in the browser's console.

Live example

Upvotes: 1

Related Questions