1

I'm trying to get my nodejs socket.io app work in my site with no results. On my own computer it works fine with xampp.

npm install --save express@4.10.2 and npm install --save socket.io are installed

Here's my code: server.js that I run with nodejs

var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );

var app = express();
var server = http.createServer( app );

var port = 8080;

var io = socket.listen( server );

io.sockets.on( 'connection', function( client ) {
    console.log("New user connected");

    client.on('message', function( data ) {
        console.log( 'Message: ' + data);

        io.sockets.emit( 'message', data);
    });
});

server.listen(port, function(){
    console.log("Listening on port: " + port);
});

Here's my index.php

<script src="script/node_modules/socket.io-client/dist/socket.io.js"></script>
<script>
    var socket = io.connect( 'http://localhost:8080' );
</script>

I'm doing this differently than this because I'm using apache.

I'm getting this error no matter is the server.js running with nodejs or not I always get this same error.

enter image description here

Virtual machine is from digitalocean.

All ports was blocked but 22, 80 and 443.

I allowed port 8080 with sudo ufw allow 8080 and sudo ufw allow out 8080 but it didn't work.

ufw was disabled sudo ufw disable but didn't work.

I have no idea what to try next. How do I fix this?

Tonza
  • 650
  • 8
  • 17

2 Answers2

1

try with this. I think is a node server miss configuration. Do you check your node log?

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var config = {port : 8080};

http.listen(config.port, function() {
  console.log('listening on port',config.port)
});

io.on('connection', function(socket) {
    console.log("New user connected");
    socket.on('message', function( data ) {
        console.log( 'Message: ' + data);
        socket.emit( 'message', data);
    });
});
nerdcoder
  • 399
  • 2
  • 8
  • What log file? And that didn't work. If you look my image you see that its `http://localhost/socket.io` and I dont fave directory like that – Tonza Dec 23 '16 at 20:14
  • Thank you but i already got it workin. It was my connection I was connecting to `http:/localhost:8080` so I changed it to my public ip – Tonza Dec 23 '16 at 20:32
0

For the client side try this :

<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script type="text/javascript">
  var socket = io.connect( 'http://localhost:8080' );
</script>

For the server side try this :

app.set('port', 8080);
var server = http.createServer( app );
var io = require('socket.io').listen(server);

Or look at this: possible duplicate Node js Digitalocean Socket io connection refused

location ~* \.io {
    proxy_pass http://localhost:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;        
}
And don't forget the upstream for your node js app

upstream app_yourdomain {
    server 127.0.0.1:3000;
    keepalive 8;
}
Community
  • 1
  • 1
Ahmed Commando
  • 723
  • 2
  • 7
  • 23