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

微笑、不失礼 提交于 2019-11-30 06:57:09

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!

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!