Connect to a specific server region

Hello,

This is for photon realtime cloud with a swift client.

How do I connect to a specific server region?

Thanks,

Comments

  • Hi @GreenRollingHills.

    1.
    The Constructor of class 'EGLoadBalancingClient' has a couple of optional parameters. The last one of them, called 'regionSelectionMode', takes one of the values from 'EGRegionSelectionMode.h' and defaults to 'EGRegionSelectionMode_DEFAULT'. Please explicitly pass 'EGRegionSelectionMode_SELECT' for that parameter.

    2.
    During the connect flow that is triggered by your call to 'EGLoadBalancingClient::connect()', the client receives a list of available regions from the name server.
    'EGLoadBalancingListener' declares an optional callback 'EGLoadBalancingListener::onAvailableRegions()'. If you have passed 'EGRegionSelectionMode_SELECT' for 'regionSelectionMode', then 'EGLoadBalancingClient' won't automatically choose an item in that list of available regions, but pass the list to that callback.
    Therefor in your 'EGLoadBalancingListener' implementation you should override the empty default implementation of that callback with a meaningful implementation that selects a region based on whatever criteria you can come up with.

    3.
    The connect flow pauses entirely until you have chosen a region.

    4.
    Pass your chosen region to 'EGLoadBalancingClient::selectRegion()' to continue the connect flow.

    Note:
    'EGLoadBalancingClient::selectRegion()' is only expected to be called after you have received a call to 'EGLoadBalancingListener::onAvailableRegions()' (call 'selectRegion()' either directly from within this callback or later, after the callback has returned). Otherwise the Client won't be at the correct stage of the connection-flow for region selection.

    An example implementation of 'EGLoadBalancingListener::onAvailableRegions()' can be found in the source code of demo_loadBalancing_objc inside the Client SDK:
    - (void) onAvailableRegions:(EGArray*)availableRegions :(EGArray*)availableRegionServers
    {
    	NSString* r = [availableRegions componentsJoinedByString:@", "];
    	NSString* s = [availableRegionServers componentsJoinedByString:@", "];
    	EGLOG(EGDbgLvl::INFO, L"onAvailableRegions: %ls / %ls", [r UTF32String], [s UTF32String]);
    	[mOutputListener writeLine:@"onAvailableRegions: %@ / %@", r, s];
    	// select first region from list
    	[mOutputListener writeLine:@"selecting region: %@", availableRegions[0]];
    	[mLoadBalancingClient selectRegion:availableRegions[0]];
    }
    Note that 'selectRegion()' is only supposed to be called during the connect-flow after the call to 'onAvailableRegions()' has been received. Calling it at any other time or state or calling it multiple times for a single call to 'onAvailableRegions()' is not supported and produces undefined behavior.
  • Hello

    So, it looks like I can successfully connect by following the above instructions. However, I am unsure how to verify that I connected to the correct server. Is there a way to ask the client after connection which server region it connected to?

    Thanks,
  • Hi @GreenRollingHills.

    No, there is no way to do that as it is unnecessary. The client will only try to connect to the region that you have specified. It either successfully connects to the correct region or it fails to connect entirely in which case you receive a connection error. So when you have successfully connected after after seelcting a region, it is guaranteed that you are connected to this exact region.

    For debugging purposes you could inspect the value of 'mSelectedRegion', which is a private member variable of the Client instance. There is no public API to access it, but you could still inspect it in the debugger or add a log call that prints it out inside Client.cpp during development.
  • Thanks for the responses. These answer my questions.