Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js TCP Socket Server on the Cloud [Heroku/AppFog]

Is is possible to run a Node.js TCP Socket oriented application on the Cloud, more specifically on Heroku or AppFog.

It's not going to be a web application, but a server for access with a client program. The basic idea is to use the capabilities of the Cloud - scaling and an easy to use platform.

I know that such application could easily run on IaaS like Amazon AWS, but I would really like to take advantage of the PaaS features of Heroku or AppFog.

like image 988
Itay Grudev Avatar asked Nov 19 '12 17:11

Itay Grudev


3 Answers

I am reasonably sure that doesn't answer the question at hand: "Is is possible to run a Node.js TCP Socket oriented application". All PaaS companies (including Nodejitsu) support HTTP[S]-only reverse proxies for incoming connections.

Generally with node.js + any PaaS with a socket oriented connection you want to use WebSockets, but:

  1. Heroku does not support WebSockets and will only hold open your connection for 55-seconds (see: https://devcenter.heroku.com/articles/http-routing#timeouts)

  2. AppFog does not support WebSockets, but I'm not sure how they handle long-held HTTP connections.

  3. Nodejitsu supports WebSockets and will hold your connections open until closed or reset. Our node.js powered reverse-proxies make this very cheap for us.

We have plans to support front-facing TCP load-balancing with custom ports in the future. Stay tuned!

like image 110
indexzero Avatar answered Nov 11 '22 20:11

indexzero


AppFog and Heroku give your app a single arbitrary port to listen on which is port mapped from port 80. You don't get to pick your port. If you need to keep a connection open for extended periods of time see my edit below. If your client does not need to maintain and open connection you should consider creating a restful API which emits json for your client app to consume. Port 80 is more than adequate for this and Node.js and Express make a superb combo for creating APIs on paas.

AppFog

https://docs.appfog.com/languages/node#node-walkthrough

var port = process.env.VCAP_APP_PORT || 5000;

Heroku

https://devcenter.heroku.com/articles/nodejs

var port = process.env.PORT || 5000;

EDIT: As mentioned by indexzero, AppFog and Heroku support http[s] only and close long held connections. AppFog will keep the connection open as long as there is activity. This can be worked around by using Socket.io or a third party solutions like Pusher

// Socket.io server
var io = require('socket.io').listen(port);
...
io.configure(function () { 
  io.set("transports", ["xhr-polling"]); 
  io.set("polling duration", 12); 
});
like image 45
Tim Santeford Avatar answered Nov 11 '22 22:11

Tim Santeford


tl;dr - with the current state of the world, it's simply not possible; you must purchase a virtual machine with its own public IP address.

All PaaS providers I've found have an HTTP router in front of all of their applications. This allows them to house hundreds of thousands of applications under a single IP address, vastly improving scalability, and hence – how they offer application hosting for free. So in the HTTP case, the Hostname header is used to uniquely identify applications.

In the TCP case however, an IP address must be used to identify an application. Therefore, in order for this to work, PaaS providers would be forced to allocate you one from their IPv4 range. This would not scale for two main reasons: the IPv4 address space having been completely exhausted and the slow pace of "legacy" networks would make it hard to physically move VMs. ("legacy" networks refer to standard/non-SDN networks.)

The solution to these two problems are IPv6 and SDN, though I foresee ubiquitous SDN arriving before IPv6 does – which could then be used to solve the various IPv4 problems. Amazon already use SDN in their datacenters though there is still a long way to go. In the meantime, just purchase a virtual machine/linux container instance with a public IP address and run your TCP servers there.

like image 27
jpillora Avatar answered Nov 11 '22 21:11

jpillora