views:

348

answers:

2

Hey I am writing an app in Twisted, and as it stands I have 4 servers bound two different ports all communicating with the client via JSON. Is there anyway to bind these 4 servers to the same port and have the interactions remain the same?

For instance say the client subscribes to two different feeds, transmitted via a direct socket.

Right now I just do like

server1.read_string()
server2.read_string()

and it will read the correct JSON string from the respective feeds. Is there anyway to maintain this type of functionality but contact my server on the same port?

I do not want to throw all of the server functionality into one massive server and partition the data by header prefixes.

I don't want to do something like

s = server.read_string()
header = s.split(//some delimiter)[0]
if (header == "SERVER1")
{
   // Blahh
}
+1  A: 

In order to have multiple servers running on the same machine all bound to the same port, they need to be bound to different IP addresses. The only way to bind to the same port on the same IP is to enable the socket's SO_REUSESOCKET option, but then multiple servers would be able to receive each other's inbound data, really messing up your communications.

Otherwise, having a single server that uses headers to identifies the particular feeds is best. Why do you not want to do that?

Remy Lebeau - TeamB
I would need the headers on the client side as well yes? Is there a better way to transmit header data than directly in the body of the message like I have shown above?
DevDevDev
The header has to be in the message data itself. When a message is received on either end, the receiver can identify which feed the message relates to.
Remy Lebeau - TeamB
+1  A: 

It sounds like you have many clients interacting with your servers via HTTP. The standard solution is to throw a reverse proxy between the client and your servers - that proxy then forwards connections to the appropriate server depending on the URL. The reverse proxy can run on any one of your existing servers or on its own server to lighten the load.

If your data is cachable, the reverse proxy can do caching on your results too.

There are many reverse proxies available and you will want to choose one based on what sort of workload you have. Do you need it to be highly configurable? Is the data public or based on logins? How long does each connection last / how many connections to you want to hold open at once?

Squid, Varnish, HAProxy are good reverse proxies and even Apache could do this for you.

I plan to use HAProxy for Gridspy, my project as I have many ongoing connections with my clients and want to place an orbited server in the same URL path as my django server. See This tutorial for more information on how to forward many connections on port 80 from one server to many. This tutorial is focused on Comet, but your problem is even simpler than that.

If you are considering an ongoing tcp/ip connection from the browser back to your servers, seriously consider Orbited. See this tutorial about graphs via orbited and morbidQ. Orbited will also punch through firewalls and proxies better than most custom solutions will, as it looks like normal HTTP traffic.

Tom Leys