bezzoon
bezzoon

Reputation: 2019

Socket io and Node works locally but not working on heroku

The code works locally. When I push it to heroku there are problems.

I am certain the ports are right. I don't know what the issue is. I suspect that it's an issue with the client side code. I tried changing it to https:// as opposed to http:// but that made it stop working locally and on heroku.

Error on FF:

Firefox can't establish a connection to the server at ws://localhost/socket.io/?EIO=2&transport=websocket.

Error on Chrome:

WebSocket connection to 'ws://localhost/socket.io/?EIO=2&transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 404 

//on the client side
socket = io.connect("http://localhost", {port: PORT, transports: ["websocket"]});


//on the server side
var port = Number(process.env.PORT || 4000);
var express = require("express");
var logfmt = require("logfmt");
var app = express();
app.set('views', __dirname + '/views');

var server = require('http').createServer(app)
  io = require("socket.io").listen(server);

  io.set("transports", ["websocket"]);
  io.set("polling duration", 10)

https://bitbucket.org/hassanshaikley/amara-game/src/27b720f463f18b3ca1864fc1028d86e72928b126/scripts/game.js?at=master

let me know if I am missing details please. I am a little stumped.

Upvotes: 2

Views: 2010

Answers (1)

barodeur
barodeur

Reputation: 46

First, if you want your client to connect to you heroku app, you should use your heroku app host instead of localhost.

var host = location.origin;
io.connect(host, {port: PORT, transports: ["websocket"]});

Upvotes: 3

Related Questions