Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket io not working after being deployed

I'm not getting an error but io.on('connection', function(socket){ console.log('a user connected', socket.client.id); }); doesn't seem to work when I deployed my application in Linode.

But in the development it's working fine.

server.js ( socket io )

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

var port = 8002;

io.on('connection', function (socket) {
    console.log('a user connected', socket.client.id); // triggering in the development
});

http.listen(port, function () {
    console.log('Express server listening on port ' + port);
    console.log('env = ' + app.get('env') +
        '\n__dirname = ' + __dirname +
        '\nprocess.cwd = ' + process.cwd());
});

client html ( note: i change the localhost to my web app ip)

  <script src="http://localhost:8002/socket.io/socket.io.js"></script>

and my application seem to be using the socket.io polling-xhr.js

enter image description here

I'm kind of new to this.

like image 830
Code.Freeze Avatar asked Jul 15 '26 10:07

Code.Freeze


1 Answers

I don't know if this will fix your particular problem, but in order to set up socket.io on the same port that your application uses you can configure it this way:

const express = require('express');
const app = express();
const http = require('http');
const hostname = 'localhost';
const port = 80;

// the express app is registered with the server
const server = http.createServer(app);

// setup socket.io and register it with the server
const io = require('socket.io').listen(server);

// tell the application to listen on the port specified
server.listen(port, hostname, function (err) {
  if (err) {
    throw err;
  }
  console.log('server listening on: ', hostname, ':', port);
});

Now socket.io runs on the same port as your application.

like image 149
Porlune Avatar answered Jul 20 '26 06:07

Porlune



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!