Skip to content

CtxVirtualSite()

ts
function CtxVirtualSite(): string;

Returns the ID of the virtual site in whose context the event that triggered this script occurred. Unlike every other Ctx* function, this one always returns a meaningful value: it is never an empty string, for any event.

Why this function exists, and why it matters

Every worker instance serves exactly one virtual site, and it will not even start without one, so the virtual site is known before the first connection is accepted and cannot change for the life of the session. That makes CtxVirtualSite() the one context value you never need to guard against being empty: there is no event, not even OnNewConnection or BeforeSendSoftwareID, that fires outside a virtual site.

Like every Ctx* value, it is captured at the moment the event fires and frozen into the script's execution context for its entire lifetime. It is equivalent to Session.GetVirtualSite(), but it is available without going through the session object, which keeps it usable in exactly the same way as the rest of the event context.

This is most useful in multi-tenant deployments, where the same script is shared across virtual sites and must branch on, or simply record, which tenant it is running for.

Return value

ScenarioReturn value
Any event, at any point in the sessionThe virtual site ID, e.g. 0ujsszwN8NRY24YaXiTIE2VWDTS

NOTE

This is the virtual site ID, not its display name, exactly like the value returned by Session.GetVirtualSite().

Example

ts
{
  Log("Event fired in virtual site: " + CtxVirtualSite());

  if (CtxVirtualSite() === "0ujsszwN8NRY24YaXiTIE2VWDTS" && CtxUsername() !== "") {
    Log("Tenant-specific handling for user: " + CtxUsername());
  }
}

WARNING

CtxVirtualSite() is not available prior to v8.0.6. Please upgrade to v8.0.6 or newer to use this function.

See also