socket io connection working fine in localhost but not when deploying on heroku server - node.js

When I am running my code on localhost it is working fine but after deploying on heroku it shows error (GET Error)
Here Is My Code
Server Side Code
app = express()
app.listen(app.get('port'), function() {
console.log('Node app is running on port..', app.get('port'));
});
var server = app.listen(4200);
var io = require('socket.io')(server);
io.on('connect',(socket)=>{
console.log('connected..........');
})
Client Side Code
private socket = io('My-Heroku-server-address:4200');
Error
https:My-Heroku-server-address:4200/socket.io/?EIO=3&transport=polling&t=MDpJszb

Instead of specifying the URL to connect in the client side code like private socket = io('My-Heroku-server-address:4200'); just do
var socket = io();
This will try to connect to the host that serves the page.
Refer Socket.IO for more information.

Thanks all.
Problem has been solved.
I just removed the port no.
private socket = io('My-Heroku-server-address');
Thanks all

Related

'wss://' NodeJS Connection

I am using BinaryJS to stream to a server. But at this moment I put my page in HTTPS (OPENSSL). This is why all create the connection in the client:
var client = new BinaryClient('wss://my_IP:9050');
If I use ws the browser will give error because this is not a protected connection and the page use https. But I am having an error anyway:
WebSocket connection to 'wss://my_IP:9050/' failed: Error in connection establishment: net::ERR_CONNECTION_CLOSED
And the server Code is:
var BinaryServer = require('binaryjs').BinaryServer;
binaryServer = BinaryServer({ port: 9050});
Should the binaryserver using some certificate? I cant understand the reason of the error.
The solution is here:
binaryServer = BinaryServer({
//port: 9000 //BinaryServer will share the same port of httpsServer
server: serverHTTPS, //Server previously defined
});
Based on this link

Socket.IO connection error

I've an application in node.js with socket.io. Everything was running completely fine, but suddenly the browser started to send this error.
failed: Error in connection
establishment:net::ERR_TUNNEL_CONNECTION_FAILED
I didn't make any code change.
The protocol used by socket is ws:// and when I try to use this url in browser
'ws://highgarden-nodejs-91180.sae1.nitrousbox.com/socket.io/?EIO=3&transport=websocket&sid=T9Unec8KbWw-GAL8AAAF'
Chrome returns this error:
Failed to load resource: net::ERR_DISALLOWED_URL_SCHEME
This is a part of the socket setup code:
server.js:
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var port = process.env.PORT || 3000
*------------------------------------------*
// routes ===
var routes = require('./config/routes.js');
var sock = {}
routes(app, passport, sock);
io.sockets.on('connection', sock.update);
// launch ===
server.listen(port);
Thanks advance.
Hi the exception ERR_TUNNEL_CONNECTION_FAILED happens when a tunnel connection through the proxy could not be established. And the ERR_DISALLOWED_URL_SCHEME happens when the scheme of the URL is disallowed.
May you need use it behind the proxy!
Chrome 45.0.2454.101 m says the page has been disabled or moved on the server.

Why won't my app establish websocket connection on Heroku?

I am trying to deploy my nodejs app on heroku. I cannot get the websocket connection to establish. I have read everything I could find, tried every example and none of them seem to work for me.
I when I try to open my page "heroku open", nodejs is correctly giving me the html file. However, the websocket 'ws' connection never establishes.
My server.js has these configurations:
var pport = process.env.PORT || 5000;
server.listen(pport, function(err) {
if(!err) { console.log("Listening on port " + pport); }
});
Side Note
When I check "heroku logs", I find that the port my app is running on is a random 5 digit number. ( like 28896, 53365, etc.) It never actually runs on the second half || 5000.
But the thing is, in order for my game.html file to establish a websocket connection, it needs to know what port.
I have tried the following client configurations, none have worked:
1)
var host = location.origin.replace(/^http/, 'ws');
this.connection = new WebSocket(host);
2)
var host = location.origin.replace(/^http/, 'ws');
host = host + ":5000";
this.connection = new WebSocket(host);
3)
this.connection = new WebSocket('ws://infinite-earth-7708.herokuapp.com/');
I have also done what their website said, and attempted to use the following after deploying my app:
heroku config:add NODE_ENV=production
Please advise
Well I figured it out. Here is what you should know:
I did not change my server configurations from my original post.
My client configurations looked like this:
var host = location.origin.replace(/^http/, 'ws');
this.connection = new WebSocket(host);
But here is the kicker.
On the terminal I used the following command:
heroku labs:enable websockets
And voila, it works! I hope this helps someone.

AppFog node.js client cannot load socket.io

I just deployed a node.js site to AppFog. It works fine locally when I go to "http://localhost:8080", but when I got to the one on AppFog: http://bandwithfriends.aws.af.cm/ I get the following Chrome console error:
XMLHttpRequest cannot load http://localhost:8080/socket.io/1/?t=1357769454152. Origin http://bandwithfriends.aws.af.cm is not allowed by Access-Control-Allow-Origin.
SERVER CODE:
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
static = require('node-static'); // for serving files
// This will make all the files in the current folder
// accessible from the web
var fileServer = new static.Server('./');
app.listen(process.env.VCAP_APP_PORT || 8080);
io.configure('development', function(){
io.set('transports', ['xhr-polling']);
});
CLIENT CODE index.html:
I am including socket.io like this:
<script src="/socket.io/socket.io.js"></script>
CLIENT CODE script.js:
var url = 'http://localhost:8080';
var socket = io.connect(url);
How do I fix this?
All I had to do to fix this was remove the url on the client side code:
var socket = io.connect();
Hope this helps anyone else having the same problem!
Both your client side and server side are wrong.
On the client side; make this change:
var url = io.connect('http://<your domain name>');
//For example: var url = io.connect('http://shash7.ap01.aws.af.cm');
On the server side, you need to implement socket.io like this:
var app = require('express')() , server =
require('http').createServer(app) , io =
require('socket.io').listen(server);
server.listen(80);
The above code was taken from the socket.io site. They changed the way socket.io listens in the 0.9 version so use the above version.
Other than that, everything looks fine. Tell me if you still can't figure this out.

Simple socket.io example not work

This is server file
var io = require('socket.io').listen(7000);
io.sockets.on('connection', function(socket) {
console.log("### new connection ###");
});
This is for client
$j(document).ready(function() {
var socket = io.connect('http://address.net:7000');
console.log(socket);
});
Server starts correct without any errors.
I copied from node_modules and attach to my site < head > section file "socket.io.min.js" file for client from dist folder.
And in output of server isn't any ### new connection ### when I run my site.
Why?
Problem resolved. It was about wrong server address in socket.io connection function.

Resources