Node.js hosting with Socket.io client support? - node.js

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

Related

Host a Node.js bot (express and botkit)

I just made a bot in node.js for the Cisco Webex Teams application. My bot uses "express" and "botkit". "Express" requires listening on the port "3000" and "Botkit" listening on the port "8080".
I tried heroku.com but it does not accept two predefined ports and does not save files dynamically (fs.write)
var PUBLIC_URL = "http://a796e3b7.ngrok.io";
var port ='3000';
var ACCESS_TOKEN ='xxx';
var SECRET = "xxx";
var express = require('express');
var app = express();
var Botkit = require('botkit');
var controller = Botkit.webexbot({
log: true,
public_address: PUBLIC_URL,
access_token: ACCESS_TOKEN,
secret: SECRET,
webhook_name: process.env.WEBHOOK_NAME || 'Email2Webex',
});
controller.setupWebserver(8080, function(err, webserver) {
controller.createWebhookEndpoints(webserver, bot, function() {
console.log("Webhooks set up!");
});
});
app.post('/mailgun', upload.any(),function(req, res, next){
res.end('ok');
});
app.listen(port);
Currently I use ngrok to host the bot locally on my computer and I want to be able to host it on a server so I do not have to worry about it. how can I do ?
You can't set the port on Heroku apps. Heroku sets the port you're supposed to use through the PORT environment variable, and you should use it via process.env.PORT. Generally speaking, deployed applications should not run on development ports like 8080 - if it's an HTTP server, it must listen on port 80, for example.
In order to have two apps listening at the same time, I suggest you refactor your code and include both your bot and your app into a single express server that will listen at the port defined by Heroku's PORT environment variable.
Concerning access to the file system, it is borderline possible to use it, but there are high security restrictions, so a code that might run on your machine is likely to break on the server. Generally speaking it's a bad idea to access the file system directly in Heroku, except for read-only actions on deployed files. That is in part because the file system is ephemeral, so dont assume your written files will always be there. Most issues related to the caveats of using the file system can be resolved by using database or file storage features provided by Heroku, though.

Networking websockets on node.js servers

So I've been coding in web design for two weeks now and I've devolved the core for my io game on node.js just by using localhost:3000 now I'm trying to implement what I have so far into an actual web-server. It's one heck of a learning curve, so say I set up a virtual-machine in Google Cloud Platforms running node.js, socket.io what do I even set my ports too?
This is my Code currently server side:
var express = require('express'); //adds express library
var app = express();
var server = app.listen(3000); //listens on port 3000
app.use(express.static('public')); //sends the public(client data)
console.log("Server Has Started");
var socket = require('socket.io'); //starts socket
var io = socket(server);
This is my Code currently client side:
var socket = io.connect("http://localhost:3000")
my website is gowar.io and it currently resides as a static file in googles "bucket". How do I hook up my websockets with something like a virtual machine?
Typically, cloud ecosystems will give you an endpoint for your storage or allow you to configure one.
Skim through Google's Docs about WebSockets to learn more about their recommended implementation of WebSockets.

Connecting to NodeJS server from apache served page

So basically I have a webpage runned by apache on port 1900 and I have a NodeJS server running on port 3000.
Server code:
var express = require('express');
var http = require('http');
//make sure you keep this order
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var port = 3000;
server.listen(port, '192.168.0.105', function(){
console.log('Server started: listening on port '+port+'.');
});
On the webpage a have the following code:
var socket = io('192.168.0.105:3000'); which connects to the NodeJS server when loading the page from the computer that runs the server(my laptop) and apache.
The problem appears when I try to access the webpage from another computer(laptop) connected to the same LAN that the laptop running the server is.
When I access 192.168.0.105:1900 from that laptop, I only see the page that is being loaded from apache but doesn't connect to the NodeJS server, it tries to connect to 192.168.0.105:3000 forever but fails after 1 minute.
How do I resolve this problem?
Thank you.
Making my comment into an answer since it solved the problem.
Windows 7 has a built-in personal firewall by default. You may have to enable connections to port 3000 manually. The router is presumably for access from outside the network. You're talking about accessing from your laptop when on the same LAN so that would more likely be the built-in personal firewall.

Websocket on Azure with nodeJS

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)

Serving socket.io through SSL

I'd like to implement ssl in all of my projects. One of them is using socket.io.
My code to create the socket:
var server = require('http');
var app = server.createServer(function(request, response) {
response.end();
});
var io = require('socket.io').listen(app);
app.listen(8000);
I can't bind node.js / socket.io to ssl port cause it's already in use by my webserver (cherokee). Therefore i can't serve the client using https.
Any ideas how to solve this problem?
You can only bind one application to a port. Since your web server is already bound to port 443, you have two choices:
Run the web server like you are now and proxy the node.js stuff running on port 8000:
http://www.cherokee-project.com/doc/modules_handlers_proxy.html
Run node.js on that port, change the port of the web server and proxy the web server using node.js: https://github.com/nodejitsu/node-http-proxy

Resources