Websocket on Azure with nodeJS - node.js

I manage to make websocket work on a nodeJS+express application on azure.
However it is using polling instead of websocket, anyone know why is that?
Here are the config.
Client Side
socket = io.connect(url, {'transports':['websocket', 'polling']);
Server side
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
console.info('Express server started');
});
var io = require('socket.io').listen(server, {'transports': ['websocket', 'polling']});
I am using socket.io 1.3.6
EDIT:
On Azure I have websocket and the Always On setting ON.
It's also not a the free package.

OK. I also have a socketIO app hosted on an azure website, and the web sockets does work as expected. Did you check this article out? Enabling Websockets for Socket.io Node apps on Microsoft Azure
Here's the important part:
Note that we say "webSocket enabled=false" in this web.config. This is
confusing, but makes sense when you realize we're saying "disable
Websockets in IIS and let node (or whomever) downstream handle it"

I ended up downgrading socket.io to 1.3.5 to get websockets to work on Azure (iisnode)

Related

socket.io + azure app services 503 error even though websockets is ENABLED

I am trying to set up socket.io with my Node.js server on Azure app services. It works perfectly fine in my local server. However, I can't seem to get it to work on Azure.
I have enabled web sockets in my Azure App Services -> configuration -> general settings. However, this does not work.
I have followed instructions in this stackoverflow post: Socket IO net::ERR_CONNECTION_REFUSED but it hasn't worked for me.
My server side code:
...
const port = process.env.PORT || 5000;
const app: Application = express();
const server = require("http").Server(app);
const io = require("socket.io")(server);
...
io.of("/chat").on("connection", async function(socket: any) {
console.log('hi')
});
...
server.listen(port, () => console.log(`listening on port ${port} `));
My CORS settings have also been set up appropriately, HTTP requests to my server work fine, as well as requests that require a credentials.
Any help would be appreciated. Thanks
EDIT (MORE INFO):
I am using in-house authentication. Because of this, I need to set the header 'Access-Control-Allow-Credentials' to TRUE. My express app was setting this to true via the cors npm module, however no matter what I did I could not get HTTP requests to work when doing authentication requests.
I was able to solve this by doing:
az resource update --name web --resource-group <myResourceGroupName> --namespace Microsoft.Web --resource-type config --parent sites/<site-name> --set properties.cors.supportCredentials=“true” --api-version 2015-06-01
which i found here: (https://learn.microsoft.com/bs-latn-ba/azure/app-service/app-service-web-tutorial-rest-api)
This allowed HTTP requests with credentials to work, as well as any other HTTP requests. It is important to note that in my Azure settings, all my CORS settings are still blank in order to allow my express app to handle CORS.
Not sure if this is related...
EDIT2:
when I try to connect from my client with
const socketIo = io(socketUrl + "/chat", {
// transports: ["websocket"]
upgrade: false,
})
everything works fine. But if i uncomment transports: ["websocket"], connection will fail. It's got to have something to do with the websocket settings in Azure
I would suggest you to provide a minimal working example (server and a client page) it would simplify things.
Here are a couple things to check:
Disable perMessageDeflate in your server-side Node.js code
for example like this:
const io = require('socket.io')(server,{
perMessageDeflate :false
});
Azure Web Apps only listens on ports 80 & 443 which is a common issue
It doesn't look like a case here, but it happens so often, so I will leave it here to help others. According to the doc:
Q: Can I expose more than one port on my custom container image?
A: We don't support exposing more than one port.
So, if this is a case - change the port to either of them and your app will work fine.
I hope it helps! 🙂
So I made a barebones example to test the socket connection... and it worked.
Then, I tried my app again.. and it worked. Dunno what happened but everything is working now...

Socket.io + Azure web sockets issue

I am working on a multiplayer chess game with NodeJS and socket.IO.
I have problem hosting it on Azure tho.. I tried many different approaches, a few mentioned:
Forcing the application to only use WebSockets by adding the code below:
io.configure(function() {
io.set('transports', ['websocket']);
});
Added <webSocket enabled="false"/> in web.config file..
Note: This disables the IIS WebSockets module, which includes its own implementation of WebSockets and conflicts with Node.js specific WebSocket modules such as Socket.IO. If this line is not present, or is set to true, this may be the reason that the WebSocket transport is not working for your application.
Matching origin protocol to ensure no SSL issues.
io.configure(function() {
io.set('match origin protocol', true);
});
I now started from scratch, since I thought my server-side part was corrupt, and tried Socket.io chat example instead.
I followed the steps.
Created a new web app on Azure.
Published my files through FileZilla FTP.
Enabled Web Sockets on Azure for my app (disabled by default).
STILL THE SAME ERROR! See picture below.
Anyone? I am unsure if it's a client-side or server-side issue. It seems like it's trying to XHR-poll instead of using web sockets..
Thanks in advance.
I got it working, thank you Chris Anderson-MSFT for your help.
The weird thing that occurred for me when deploying with FTP was that my node_modules folder differed with version(s) specified in my package.json.
I solved this by connecting my web app on Azure to a local Git repository and deploying the app through git. This connects my packages recursively and matches correct versions.
I also needed to enforce my client-side socket-io to use web sockets by specifying transport method:
var socket = io({transports:['websocket']});
And this is what my server-side file ended up looking like:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var port = process.env.PORT || 3000;
app.use(express.static('public'));
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/default.html');
});
io.on('connection', function(socket) {
io.set('transports', ['websocket']);
console.log('new connection on socket.io');
socket.on('move', function(msg) {
socket.broadcast.emit('move', msg);
});
});
server.listen(port, function () {
console.log('Server listening at port %d', port);
});

Socket.io-based app running through node proxy server disconnecting all sockets whenever one disconnects

I made a basic chat app using node.js, express and socket.io. It's not too different from the tutorial chat app for socket.io, it simply emits events between connected clients. When I ran it on port 3001 on my server, it worked fine.
Then I made a proxy server app using node-http-proxy which listens on port 80 and redirects traffic based on the requested url to various independent node apps I have running on different ports. Pretty straightforward. But something is breaking. Whenever anyone disconnects, every single socket dis- and re-connects. This is bad for my chat app, which has connection-based events. The client consoles all show:
WebSocket connection to 'ws://[some socket info]' failed: Connection closed before receiving a handshake response
Here's what I think are the important parts of my code.
proxy-server.js
var http = require('http');
var httpProxy = require('http-proxy');
//create proxy template object with websockets enabled
var proxy = httpProxy.createProxyServer({ws: true});
//check the header on request and return the appropriate port to proxy to
function sites (req) {
//webapps get their own dedicated port
if (req == 'mychatwebsite.com') {return 'http://localhost:3001';}
else if (req == 'someothersite.com') {return 'http://localhost:3002';}
//static sites are handled by a vhost server on port 3000
else {return 'http://localhost:3000';}
}
//create node server on port 80 and proxy to ports accordingly
http.createServer(function (req, res) {
proxy.web(req, res, { target: sites(req.headers.host) });
}).listen(80);
chat-app.js
/*
...other modules
*/
var express = require("express");
var app = exports.app = express(); //I probably don't need "exports.app" anymore
var http = require("http").Server(app);
var io = require("socket.io")(http);
io.on("connection", function (socket) {
/*
...fun socket.on and io.emit stuff
*/
socket.on("disconnect", function () {
//say bye
});
});
http.listen(3001, function () {
console.log("listening on port 3001");
});
Now from what I've read on socket.io's site, I might need to use something to carry the socket traffic through my proxy server. I thought that node-http-proxy did that for me with the {ws: true} option as it states in their docs, but apparently it doesn't work like I thought it would. socket.io mentions three different things:
sticky session based on node's built in cluster module
socket.io-redis, which allows separate socket.io instances to talk to each other
socket.io-emitter, which allows socket.io to talk to non-socket.io processes
I have exactly no idea what any of this means or does. I am accidentally coding way above my skill level here, and I have no idea which of these tools will solve my problem (if any) or even what the cause of my problem really is.
Obligatory apology: I'm new to node.js, so please forgive me.
Also obligatory: I know other apps like nginx can solve a lot of my issues, but my goal is to learn and understand how to use this set of tools before I go picking up new ones. And, the less apps I use, the better.
I think your intuition about needing to "carry the socket traffic through" the proxy server is right on. To establish a websocket, the client makes an HTTP request with a special Upgrade header, signalling the server to switch protocols (RFC 6455). In node, http.Server instances emit an upgrade event when this happens and if the event is not handled, the connection is immediately closed.
You need to listen for the upgrade event on your http server and handle it:
var proxy = httpProxy.createProxyServer({ws: true})
var http = http.createServer(/* snip */).listen(80)
// handle upgrade events by proxying websockets
// something like this
http.on('upgrade', function (req, socket, head) {
proxy.ws(req, socket, head, {target:sites(req.headers.host)})
})
See the node docs on the upgrade event and the node-http-proxy docs for more.

Adobe AIR- connection issues with socket.io

I have a desktop app in Adobe Air (flex).
I have used flashSocket.io library in my app to communicate with socket.io on node.js server.
It works perfectly for most of the clients.
But for some random client the flex app is not able to create a connection with the socket.io server. It constantly throws connection error and close error on flex. The clients are not behind any firewalls or proxy server.
The console has warning like
Web Socket Connection Invalid
I guess this for those clients who are not able to connect.
Since its working for majority of the users i don't know where should i look into. Also, i am unable to reproduce this on my side.
Here's the Server Code:
var io = require('socket.io').listen(app);
app.listen(8080, function() {
console.log('%s listening at %s', app.name, app.url);
});
io.configure(function() {
io.set('transports', ['flashsocket']);
io.set('flash policy port', 843);
});
Flex code:
socket = new FlashSocket("http://domain.com:8080/");
socket.addEventListener(FlashSocketEvent.CONNECT, onConnect);
socket.addEventListener(FlashSocketEvent.MESSAGE, onMessage);
socket.addEventListener(FlashSocketEvent.CLOSE, onDisconnect); //only close and connect_error event is always fired for certain clients
socket.addEventListener(FlashSocketEvent.CONNECT_ERROR, onConnectError);
Any help would be highly appreciated.
Use a different port, we're using 443.

Node.js hosting with Socket.io client support?

I made a socket.io client app which connects to my socket.io server and then they communicate whatever they need to.
When I do it locally on one machine or even on two different local machines, everything works fine. So I tried to deploy the client on cloud9 and it keeps throwing this error:
net.js:540
connectReq = self._handle.connect(address, port);
Error: No local connects allowed for security purposes
at connect (net.js:540:31)
at net.js:607:9
at Array.0 (dns.js:88:18)
at EventEmitter._tickCallback (node.js:190:38)
The client code is, where [ip-address] is my servers IP address:
var io = require('socket.io-client'),
socket = io.connect('[ip-address]', {
port: 1337
});
Is there a way to run such a socket.io client at c9.io?
Did they block it because of this article?
Are there any free node.js hosting solutions where one could run a socket.io client application like the one above?
Thanks.
Depending on your needs you could create a free Heroku account. You wont have access to a database, and you're limited in resources, but if the app is small enough and efficient enough it could suffice.
Nodejitsu is currently free node.js hosting solution where everything works (including socket.io)
OpenShift uses Port 8080 and Heroku 3000.
The Client code has to be like this:
// Wrong!:
// mySocket = io.connect(host, port);
// Right:
mySocket = io();
mySocket.on(....);
The Server code has to look like this:
var express = require('express'),
app = express(),
server = require('http').Server(app),
io = require('socket.io')(server);
app.use(express.static('path/to/public/html'));
server.listen(8080); // OpenShift 8080, Heroku 3000
io.on(...);
https://devcenter.heroku.com/articles/node-websockets#create-a-socket-io-client

Resources