Can an app connect to more than one server?

Hi Michael,

Good to hear your wife is happy! Marital bliss is not among the design goals of Anvil but it’s a nice bonus.

You can connect multiple Uplink scripts on different machines to your app with no problems. To select which one to call into, you can register the functions with different names. You can pass an argument to anvil.server.callable to register a function with any name you choose:

# On the server
@anvil.server.callable('custom_function_name')
def get_data():
  return [1, 2, 3, 4, 5]

# On the client
anvil.server.call('custom_function_name')

If you want to connect multiple Uplink scripts, you can generate an ID in each script and append it to each function name. If you store the IDs of each connected Upink script in a Data Table, your app knows what IDs to append to the function names when it calls them:

MY_ID = #...something..

if not app_tables.hosts.get(id=MY_ID):
  app_tables.hosts.add_row(id=MY_ID)

@anvil.server.callable("func_" + MY_ID)
def f():
  print "Hello from host %s" % MY_ID

Then in client code:

# Call function f on all hosts
for host in app_tables.hosts.search():
  anvil.server.call('func_' + host['id'])

I recommend using a UUID for the IDs - they are designed to ensure you never accidentally get the same UUID twice.

import uuid
MY_ID = str(uuid.uuid4())

For historical interest, here’s a related thread (which also shows how to use the machine name to construct the ID. This is another good way to get a unique ID, as people have said below):

4 Likes