Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Subscriptions

The Telephony SDK supports real-time subscriptions that notify your application when resources change.

Subscriptions allow your application to be notified when a number finishes provisioning or when a message is received, without polling.

Phone Number Subscriptions

Subscribe to phone number state changes (e.g., transitions to PROVISIONING or ACTIVE). Provide a unique subscription id to safely manage the subscription lifecycle.

Subscribe

const subscriber: PhoneNumberSubscriber = {
  phoneNumberUpdated(phoneNumber: PhoneNumber): void {
    console.log('Phone number updated:', phoneNumber.id, phoneNumber.state)
    if (phoneNumber.state === PhoneNumberState.Active) {
      // The number is ready to use.
    }
  },
  connectionStatusChanged(state: ConnectionState): void {
    console.log('Connection state:', state)
    if (state === ConnectionState.Disconnected) {
      // Handle disconnection: consider re-subscribing
    }
  },
}

try {
  await telephonyClient.subscribeToPhoneNumbers('my-subscription-id', subscriber)
} catch (error) {
  // Handle/notify user of error
}
final class MyPhoneNumberSubscriber: PhoneNumberSubscriber {
    func phoneNumberUpdated(_ phoneNumber: PhoneNumber) {
        print("Phone number updated:", phoneNumber.id, phoneNumber.state)
    }

    func connectionStatusChanged(_ state: ConnectionState) {
        print("Connection state:", state)
        if state == .disconnected {
            // Handle disconnection: consider re-subscribing
        }
    }
}

do {
    let subscriber = MyPhoneNumberSubscriber()
    try await telephonyClient.subscribeToPhoneNumberUpdates(
        subscriptionId: "my-subscription-id",
        subscriber: subscriber
    )
} catch {
    // Handle/notify user of errors
}

Unsubscribe

Phone Message Subscriptions

Subscribe to phone message updates. Provide a unique subscription id to safely manage the subscription lifecycle.

Subscribe

Unsubscribe

Connection State

All subscription interfaces include a connectionStatusChanged callback that notifies you when the subscription connection state changes. Your subscriber will not receive resource update events until the connection state is CONNECTED, and will stop receiving them when it transitions to DISCONNECTED.

State
Description

CONNECTED

The subscription is actively connected and receiving updates.

DISCONNECTED

The subscription is not connected.

Subscription Best Practices

  • Use unique subscription IDs. If you call a subscribe method with the same ID twice, the second call will replace the first subscription.

  • Handle disconnections. Monitor the connectionStatusChanged callback and re-subscribe if the connection is lost unexpectedly.

  • Unsubscribe when done. Always unsubscribe when your component unmounts or the user navigates away to avoid memory leaks and unnecessary network activity.

  • Keep callbacks lightweight. Subscription callbacks are invoked on the main thread. Perform heavy processing asynchronously to avoid blocking the UI.

  • Fetch full data on update. For phone messages, the subscription notification is lightweight. Fetch the full resource via getPhoneMessage when you need the complete data payload.

Last updated