0

I have deployed my code but am unable to get socket.io working in production.

I have installed the socket.io node module with the command npm install --save socket.io

Here's the relevant code in my app.js:

var debug = require('debug')('chat');
var express = require('express');
var app = express();

app.set('port', process.env.PORT || 8080);

var server = app.listen(app.get('port'), 'APP_PRIVATE_IP_ADDRESS', function() {
  debug('Express server listening on port ' + server.address().port);
});

var io = require('socket.io')(server);

I am using the browser client <script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>

Also, the following client code does not print connect:

var socket = io();
socket.on('connect', function(){
    console.log('connect');
});

I appreciate any help!

Here is my site where I've deployed this code.

Brian Daneshgar
  • 343
  • 3
  • 20
  • 1
    I think what is happening is that you are calling `app.listen` before `require('socket.io')`. Take a look at the Socket.io [docs](http://socket.io/docs/#using-with-express-3/4). – Jacob Lambert Dec 04 '15 at 05:28

1 Answers1

0

You should not use express that way. You should use http package to create a server and attach express to it by:

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

httpServer = http.createServer( app ).listen( process.env.PORT, process.env.IP || "0.0.0.0", function() {  
    // server code here
});
kaytrance
  • 2,657
  • 4
  • 30
  • 49