AppFog node.js client cannot load socket.io - node.js

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.

Related

socket io connection working fine in localhost but not when deploying on heroku server

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

Not Found error for socket.io on the client-side

I am trying to include socket.io on the client-side. I keep getting this error message in the console every 5 seconds:
GET https://example.com/socket.io/?EIO=3&transport=polling&t=Lo8ssW0 404 (Not Found)
My code:
script(src='https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.2/socket.io.slim.js')
var socketio = io.connect('https://example.com',{secure: true, port:5089})
On the server-side I have:
const socketio = app.listen(5089)
var io = require('socket.io')(socketio)
which works correctly.
What am I doing wrong?
Simply replace
var socketio = io.connect('https://example.com',{secure: true, port:5089})
by:
var socketio = io.connect()
Does this help? If so, the problem is the host or the port in your original function call.
This simplified code should work, at least if you load the client from the server that runs Socket.io as well.

Socket.io : How to exclude Flashsocket

I'm using Socket.io 0.9.16 to send notifications to my users.
Everything is working fine, expect for Firefox (version 47). This browser is obviously using the Flashsocket callback and display a nasty warning :
"Firefox has prevented the outdated plugin "Adobe Flash" from running..."
1) Why the last version of Firefox is using Flashsocket as a callback, and not websocket ?
2) I don't want to use Flash at all. I tried to disable Flashsocket by setting which transports I want to use:
Client JS :
var socket = io.connect('myIP:XXXX',
{transports : ["websocket", "xhr-polling", "htmlfile", "jsonp-polling"]});
Server JS :
var io = require('/Path/TO/socket.io').listen(XXXX);
io.configure(function () {
io.set("transports", ["websocket", "xhr-polling", "htmlfile", "jsonp-polling"]);
io.set("polling duration", 10);
});
io.sockets.on('connection', function (socket) {
etc...
But what I did above is not working. Firefox continues to use Flash and shows the warning (as Chrome is using websocket all right)
Do you have any idea what I'm doing wrong ?
PS : I know socket.io v1.x is not using Flash as a callback anymore so it would be solution to upgrade, but I can't just figure out how to adapt my script to make it work with the v1.x version, so I'd like to stay with v0.9.16 if it possible.
Thank you for your help!
I had same problem, but updating version of socket.io and node solved the problem. Sample code given below:
Client JS
socket = io.connect('ip:port', {transports : ["websocket", "xhr-polling", "htmlfile", "jsonp-polling"]});
Server JS
var http = require('http');
//creating server
var server = new http.createServer();
//setting server listening port and domain
server.listen(PORT, DOMAIN);
var io = require('socket.io').listen(server, {transports: ["websocket", "xhr-polling", "htmlfile", "jsonp-polling"]});

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.

now.js not ready

I have a bit of an issue. I'm trying to create a dynamic web app using node.js/express.js/now.js. I've done everything as shown in the small sample code at http://nowjs.com/download , with no success, the client-side now.js script hosted properly, but now.ready(..) never fires. The only differences are that I use express and my server which is used to initialze now.js is https.
Do you have any ideas which could cause it not to work?
server side:
var server = express.createServer(..);
..
server.listen(port, function() {..});
var nowjs = require('now');
var everyone = nowjs.initialize(server);
everyone.now.log = function(msg) { console.log(msg); }
client side:
<script type="text/javascript" src="/nowjs/now.js"></script>
<script type="text/javascript">
now.ready(function() { now.log('asd'); alert('asd'); });
</script>
Any help would be highly appreciated!
Best, Kornel
Well, found the answer.
Long answer: now.js has an issue when determining the communication port on which socket.io should communicate. This issue seems only to appear when using default https port (443).
I've found two solutions, the ugly one:
https://groups.google.com/forum/?fromgroups=#!topic/nowjs/8cO9D77tN2o
Basically you need to edit the source code of now.js at now/lib/fileServer.js and replace
var hostPort = options['port'] || host[1] || '80';
with
var hostPort = options['port'] || host[1] || ((request.headers.referer.split(':')[0] === 'https') ? '443' : '80');
The nicer one is to set port options to socket.io. Lucky us, this is supported by now.js:
var everyone = nowjs.initialize(server, {port: port, socketio: {transports: ['xhr-polling', 'jsonp-polling']}});
I hope that this will help others having the same issue and also hope that this behavior will be fixed later in now.js.
Best regards: Kornel
Running latest version of node and now on OSX, with Safari.
server.js
var html = require('fs').readFileSync(__dirname+'/index.html');
var httpServer = require('http').createServer(function(req, response) {
/* Serve your static files */
response.end(html);
})
httpServer.listen(8080);
var nowjs = require("now");
var everyone = nowjs.initialize(httpServer);
console.log('done');
everyone.now.logStuff = function(msg){
console.log(msg);
}
index.html
<script type="text/javascript" src="http://localhost:8080/nowjs/now.js"></script>
<script type="text/javascript">
now.ready(function(){
// "Hello World!" will print on server
now.logStuff("Hello World!");
});
</script>
done..
Start the server:
node server.js
Open your browser:
http://localhost:8080

Resources