newpatriks newpatriks - 1 month ago 20
Javascript Question + NodeJS doesn't work on Heroku

I am doing an API and it's on Heroku. But I am having some problems with the only on the heroku side, when I test it in local everything goes fine. The API is completely independent of the frontend, so they are in a different domains (and a different hosts). The problem is that on production, I don't get succeed in connect the sockets...

I have some questions, all of that are about the configuration on heroku. I know that there are some posts with some information about that, but the posts I found it was with old versions of or old versions of heroku (heroku seems to has changed the websockets stuff the past July):

  • I don't know if I need to activate something before run on heroku. I read some posts about that, but all seems to be old... I tried to activate Websockets with:
    $ heroku labs:enable websockets
    but the response that I got it was:
    ! No such feature: websockets

  • Do I have to specify a port, or Heroku has an automatic port for that?

  • Do I need two connections? One to listen the POST/GET/PUT/DELETE and another to the sockets?


var express = require('express');
var app = module.exports = express();
var port = process.env.PORT || 5000;
var port_s = 3000;
var server = require('http').createServer(express);

require('./config/socket-io')(app, server, secret);'/user', routes.users.register);


module.exports = function(app, server, secret) {
var clients = {};
console.log("initiating sockets...");
var sio = require('').listen(server);

sio.sockets.on('connection', function (socket) {
clients[] = socket;
console.log(" connection: ";
socket.emit('identification', { data : });

socket.on('newShoutOut', function(data) {
var receptor = data.idTo;
var emiter =;
//console.log(" shout out from " +emiter+ " to "+receptor);
var elem = findElement(sio.sockets['sockets'], 'id', receptor);
sio.sockets.sockets[elem].emit('privateShoutout',{ data :, from : emiter });

socket.on('disconnect', function() {
//console.log("..." + " disconnected");

function findElement(arr, propName, propValue) {
for (var i=0; i < arr.length; i++) {
if (arr[i].id === propValue)
return i;

I repeat, everything works on localhost. I tried the API on localhost:5000 and the client app on localhost:80 and all the sockets work fine.

Thank you.

Answer Source

So there's a couple things here. If you'd like to use Heroku's Websocket service (which is pretty great actually), you're going to need to rework your code to use the einaros/ws implementation of websockets--and then add the service via heroku command line.

however, since you've already coded your app to, I would simply rework how you're instantiating the library:

var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('').listen(server),

server.listen(process.env.PORT || 3000);

This should solve your issue, but let me know what the logs show. I think the hang up is that your app, and your socket are running on two different ports.

After you've created your sever, you can listen for socket events with:

io.sockets.on('connection', function(socket) { //'connection' or any other event

Hope this helps.