Creating web socket server on Azure hosting using Node.js? - node.js

I have a node application I am trying to host on Azure, that create a websocket server. I am trying to create the web socket server using the code below:
var socket = require('websocket').server;
...
server.listen(1111,function(){
console.log('Http server is listening on port 1111');
});
When I do this on my local machine it works fine, but once it's up on Azure the following line of client side javascript:
var connection = new WebSocket('ws:' + document.domain + ':1111');
connection.send(msg); //throws error
Throws the error:
InvalidStateError: An attempt was made to use an object that is not,
or is no longer, usable
Can I get the desired functionality if I'm using azure? If so, any suggestions on where to start looking for a fix?
You can see the broken app in action here.

It will work on Azure, but you need to make a change. Try this:
server.listen(process.env.port, function () {
var addr = app.address();
console.log('Server listening on http://' + addr.address + ':' + addr.port);
});
Here's a tutorial on using the chat example from Socket.IO's source in Azure:
http://www.windowsazure.com/en-us/develop/nodejs/tutorials/app-using-socketio/
It assumes you're on Windows, which isn't strictly necessary. To use Node.js on Azure from a Mac or Linux system, just publish from Git:
http://www.windowsazure.com/en-us/develop/nodejs/common-tasks/publishing-with-git/

Related

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

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.

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.

how to embed nodejs application on client website?

firstly sorry for not standard English. : D
I have a chat application using nodejs, expresss finished running on port 3000. So I want to embed in website application clients, then how?
I had to use ajax load, but can not be:
jQuery.ajax({
type:"GET",
url:"http://localhost:3000/client/",
success: function (data){
jQuery('body').append(data);
}
});
Since you are using socket.io at server side then you can get its reference at the client using below line (jquery required):
$.getScript(host+'socket.io/socket.io.js', function()
{
var clientSocket = io.connect(host);
// ... do other stuff with your socket
});
Where host is your server host name e.g. http://192.168.1.5:3000/ where your nodejs application is deployed.
For more information see here for more client - server communication exposed events.
try the example from http://book.mixu.net/node/ch3.html (it uses long-polling) once done with it use socket.io

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