Socket.io : How to exclude Flashsocket - node.js

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"]});

Related

Node js with express return connection closed before receiving a handshake response

I have a socket running in nodejs and using this socket in html page this is working fine and some times I'm receiving the error on developer console as like
failed: Connection closed before receiving a handshake response. In this time my update not getting reflect on the user screen. Actually whenever the changes updated in admin screen I written the login in laravel to store this values into the redis and I have used the laravel event broadcast and in node js socket.io read the redis value change and push the values into the user screens.
I have code in laravel as like,
Laravel Controller,
public function updatecommoditygroup(Request $request)
{
$request_data = array();
parse_str($request, $request_data);
app('redis')->set("ssahaitrdcommoditydata", json_encode($request_data['commodity']));
event(new SSAHAITRDCommodityUpdates($request_data['commodity']));
}
In this above controller when the api call receives just store the values into this redis key and broadcast the event.
In my event class,
public $updatedata;
public function __construct($updatedata)
{
$this->updatedata = $updatedata;
}
public function broadcastOn()
{
return ['ssahaitrdupdatecommodity'];
}
Finally I have written my socket.io file as like below,
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis({ port: 6379 } );
redis.subscribe('ssahaitrdupdatecommodity', function(err, count) {
});
io.on('connection', function(socket) {
console.log('A client connected');
});
redis.on('pmessage', function(subscribed, channel, data) {
data = JSON.parse(data);
io.emit(channel + ':' + data.event, data.data);
});
redis.on('message', function(channel, message) {
message = JSON.parse(message);
io.emit(channel + ':' + message.event, message.data);
});
http.listen(3001, function(){
console.log('Listening on Port 3001');
});
When I have update the data from admin I'm passing to laravel controller, and controller will store the received data into redis database and pass to event broadcast.And event broadcast pass the values to socket server and socket server push the data whenever the redis key get change to client page.
In client page I have written the code as like below,
<script src="../assets/js/socket.io.js"></script>
var socket = io('http://ip:3001/');
socket.on("novnathupdatecommodity:App\\Events\\NOVNATHCommodityUpdates", function(data){
//received data processing in client
});
Everything working fine in most of the time and some times issue facing like
**VM35846 socket.io.js:7 WebSocket connection to 'ws://host:3001/socket.io/?EIO=3&transport=websocket&sid=p8EsriJGGCemaon3ASuh' failed: Connection closed before receiving a handshake response**
By this issue user page not getting update with new data. Could you please anyone help me to solve this issue and give the best solution for this issue.
I think this is because your socket connection timeout.
new io({
path:,
serveClient:,
orgins:,
pingTimeout:,
pingInterval:
});
The above is the socket configuration. If you are not configuring socket sometime it behaves strangely. I do not know the core reason, but i too have faced similar issues that implementing the socket configuration solved it.
Socket.io Server
Similar configuration should be done on the client side. There is an option of timeout in client side
Socket.io Client
For example.
Say this is your front-end code
You connect to the socket server using the following command:
io('http://ip:3001', { path: '/demo/socket' });
In your server side when creating the connection:
const io = require("socket.io");
const socket = new io({
path: "/demo/socket",
serveClient: false /*whether to serve the client files (true/false)*/,
orgins: "*" /*Supports cross orgine i.e) it helps to work in different browser*/,
pingTimeout: 6000 /*how many ms the connection needs to be opened before we receive a ping from client i.e) If the client/ front end doesnt send a ping to the server for x amount of ms the connection will be closed in the server end for that specific client*/,
pingInterval: 6000 /* how many ms before sending a new ping packet */
});
socket.listen(http);
Note:
To avoid complication start you http server first and then start you sockets.
There are other options available, but the above are the most common ones.
I am just describing what i see in the socket.io document available in github.socket_config. Hope this helps

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 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.

Openshift 3 Socket io

I have uploaded a site in openshift, everything seems to be working except socket io.
I can see the network tab that polling is taking place but no action is happening.
Here is my server side socket code
port= process.env.OPENSHIFT_NODEJS_PORT || 8080
io = require('socket.io').listen(applisten);
io.configure(function(){
io.set("transports", ["xhr-polling"]);
});
and below is my client side code to connectto socket.io
socket = io.connect("http://something:8000");
Thanks for any help :)
I have the same issue after socket.io changed to version 1.0.
Here's the changes that will make it work again:
io = require('socket.io').listen(applisten);
io.set('transports', [ 'polling', 'websocket' ]);
On client side:
socket = io();
Correct me if I'm wrong, another side effect from the changes I realised from the changes was that this function does not work:
socket.on('connect', function(data){
});
I change the keyword 'connect' to another something else and it works.
Hope that helps.

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.

Resources