How to deploy a socket.io node.js application on windows azure? - node.js

I am building windows azure application which is primarily based on .NET, but I also have to build a socket.io server using node.js hence i need to deploy a socket.io server and use this socket.io url to connect in my .NET application.
I followed all the steps listed here . And I am able to get the socket.io running on my local but when i deploy to cloud, it doesnt start. Please find below a code snippet for socket.io
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server, { origins: '*:*' });
server.listen(4001);
When i hosted it in my local emulator, 127.0.0.1:81 was pointing to this in my browser
But 127.0.0.1:4001 showed "Cannot GET /" on the browser, which is an indicative that the socket.io server is running on that url.
But when i deploy the same to cloud, i get the same as the screenshot on the url where the cloud service is hosted but on port 4001 where the socket.io server should have started it says page cannot be displayed.
Please let me know if you need to see any other files like web.config etc.
I have been stuck on this issue from forever and its really crucial for my project, any suggestions or ideas would be deeply appreciated.
Thanks

The important part that you are missing from the sample is setting of the port number
var port = process.env.port || 1337;
and
.listen(port)
when you are running inside of the Azure environment (even emulated) the ports are assigned for you, the port environment variable will tell you where. 4001 is likely not the assigned port.
The 1337 would only be used if you are running by executing
node server.js
from the command line

Related

Azure App Service - Node App - Do i need to createServer and required files

I have a node app, that doesn't expose any port. It's just running some tasks in the background and returning some stuff onto the console that I occasionally need to look at.
I've deployed this onto Azure App Service - however it doesn't seem to run - in the logs I see
Waiting for response to warmup request for container xxx
I was wondering in my index.js I don't actually expose any port - namely there isn't a const server = http.createServer(). Is this required from Azure's side or can I disable it?
So my index.js literally looks like:
(async () => {
// Check some things and do something
}
Also, in the https://github.com/Azure-Samples/nodejs-docs-hello-world sample project there are the files:
web.config
process.json
Do I need these for the node app to run on Azure?
And finally, in teh sample node app above, it has const port = process.env.PORT || 1337; but where in Azure's portal is that being set? Or if its defaulting to 1337, how does Azure know that?
Any help appreciated.
Thanks.
If you just want to run some tasks in the background, you can use webjob instead. It is also very convenient to check the output.
By the way, the only ports open for Web Apps are 80 and 443. We should use process.env.PORT for nodejs app port. 1337 is for local test.

Azure web deployment not displaying web page

I'm writing a simple nodejs app to be deployed to azure.
The app works fine, 100%, but the web page I have to manage admin matters refuses to load.
It always displays internal server error. I'm using express and viewing the logs but they say nothing useful. The app doesn't crash so I cant understand why it wont display.
This is the simple hello world code I'm using for testing, and even that wont display.
var express = require('express');
var app = express();
app.use(express.static('website'));
app.listen(80);
Does anyone have any idea what could be the problem?
Edit: I forgot to include that I do set the port environment variable
var port = process.env.PORT || 80;
var baseHost = process.env.WEBSITE_HOSTNAME || 'localhost';
Thats at the top of my server.js, sorry about that.
guess your app is listening on the wrong port when running on Azure App Service.
you will have to get the port number from environment variable "process.env.port"
var app = require('express')();
var port = process.env.port || 8080; // 8080 for local or whatever number u want
var listener = app.listen(port, function(){
console.log('Listening on port ' + port);
});
Follow this blog to get a Node.js app working on Azure. I used this for a demo recently and it worked properly.
Create a Node.js web app in Azure App Service
You have to use GIT and Continuous Deployment to get Azure App Services to recognize and setup the right pieces for your node application to function, it also takes care of node packages for you automatically.
Hope this helps, TehDude
Try turning on logging to get a better idea of what's going on
From How to debug a Node.js web app in Azure App Service:
To enable the logging of stdout and stderr streams, you must create an
IISNode.yml file at the root of your Node.js application and add the
following:
loggingEnabled: true
This enables the logging of stderr and stdout from your Node.js
application.
The IISNode.yml file can also be used to control whether friendly
errors or developer errors are returned to the browser when a failure
occurs. To enable developer errors, add the following line to the
IISNode.yml file:
devErrorsEnabled: true
Once this option is enabled, IISNode will return the last 64K of
information sent to stderr instead of a friendly error such as "an
internal server error occurred".
Node.js application running on Azure Web Apps Service, is hosted on IIS handled mapping via IISNode, which gives a Named Pipe to receive the incoming requests, not a TCP port like you would use when running locally.
This Named Pipe has been defined as the port in Node.js runtime on Azure Web Apps. You can define the port in your app like: process.env.PORT || 3000, with which your app can run on Azure or locally.

AZURE + SOCKET.IO: How do I know which port my Web App is using?

I went to a Hackathon last weekend and a Microsoft recruiter set me up with Azure for my Node.js project.
We used Socket.io with my project and had a hard time connecting the Client to the Server because we didn't know which port to connect to...
On our WebApp (not Azure VM), we had the following code:
var port = process.env.port || 3000;
On the client side of Socket.io, I had to specify an ip address to use along with it's port. I tried:
var socket = io('http://IP.AD.DRE.SSS:3000'); //And
var socket = io('http://IP.AD.DRE.SSS'); //And even a different Port
var socket = io('http://IP.AD.DRE.SSS:9999'); //And 443 and 80
And every iteration... I had to be doing something wrong. We ended up switching over to Digital Ocean because I knew how to use it but I really wanted to get this working.
Any Ideas?
UPDATE:
I changed it to 80 and my current error is: "Access Control Allow Origin." Note: My client is running on a server.
UPDATE 2: Return of the OP
Unfortunately, the CORS package for Node did not do the trick...
Some more info:
I'm not using Express or Connect. My server is on Azure (as an Azure Web App). My Client was on Localhost (Thanks to WebStorm).
Azure Web Apps only listens on ports 80 & 443. Change the port to either of them and your app will work fine.
Just leave the port off in the client string. You can connect using the URL alone. Look at my code I codefoster.com/commandmonkey as an example.

Why won't simple node.js app doesn't respond on Windows Azure VM?

I am new to node.js, so hopefully I'm missing something obvious.
I have a Windows Azure VM running Windows Server 2012. It has IIS installed and simple, static sites returning static HTML works fine.
I have installed node.js on this server (via Chocolatey). I've created a simple Hello World node.js application (test.js):
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('Hello World');
});
app.listen(80);
I fire this up on the server via: node test.js
It works fine on the server when I browse via http://localhost/test.js
It is unreachable from my client machine's browser via http://<servername>/test.js
I have:
created an endpoint on my VM to allow port 80 via TCP;
created a firewall rule on my VM to allow port 80 traffic
a web site on IIS for port 80 and it's running
When I change the above code to listen on a different port (e.g. 2368) and make the appropriate endpoint and firewall rules, everything works great both on the client and the server. I have no problem accessing the site.
What am I missing with port 80 here? Why can't I access my test file via port 80, but I can access it via a different port?
Hopefully it's something obvious. Thank you in advance.

Error binding socket on heroku , not sure about using express

this is my first node.js and socket.io application , i didn't use express ,I want to deploy the application on heroku do i need to use it ? i mean i just did npm install socket.io on localhost and in my server file i.e game.js i have io = require("socket.io") and socket = io.listen(Number(process.env.PORT)) only and in one of the files where from where i am sending the message i have socket = io.connect();
so please tell me if i need to use express and how show i modify my existing application ?
I have given the link to the source of application
( https://github.com/bitgeeky/herokutest )
Although the Application works fine on localhost by changing the port no , to some port no like (8000) but Heroku error log on doing "heroku open" is http://pastebin.com/MtB0z5vQ
I noticed that you haven't created a http server. I am assuming that you are creating a web application, since you are deploying to heroku. For that, you need to create a http server in nodejs.
Go through socket.io https://github.com/LearnBoost/socket.io
Also http://socket.io/#how-to-use
This should get you started
Note: You do not need express. But it will make your work easier in many ways. Depends on the type of application that you want to create.

Resources