Watch / Unwatch channels

Use Connect() to receive messages on a channel without joining as a member. Use Disconnect() to stop receiving messages without leaving.

Watch a channel​

Connect() subscribes to a channel. After connecting, the OnMessageReceived event is triggered when a message is received. Handle this event to receive messages.

Method signature​

1channel.Connect()

Event signature​

1public event Action<Message> OnMessageReceived;

Event handler signature​

1void EventHandler(Message message)
* required
ParameterDescription
message *
Type: Message
Default:
n/a
The received message.

Output​

This method doesn't return anything. To receive a message, you must handle the OnMessageReceived event.

Sample code​

Start receiving messages on the support channel.

1

Unwatch a channel​

Disconnect() lets users unwatch a given channel and its messages.

Method signature​

1channel.Disconnect()

Output​

This method doesn't return anything.

Sample code​

Stop receiving messages from a channel after receiving the first message.

1

Manage all subscriptions​

Use these methods on the Chat object to reconnect or disconnect all active channel subscriptions at once, and to monitor overall subscription status.

Reconnect all subscriptions​

ReconnectSubscriptions() restores all active channel subscriptions after a disconnect.

Method signature​

1chat.ReconnectSubscriptions()

Output​

An awaitable Task.

Disconnect all subscriptions​

DisconnectSubscriptions() drops all active channel subscriptions at once.

Method signature​

1chat.DisconnectSubscriptions()

Output​

An awaitable Task.

Stream subscription status​

StreamSubscriptionStatus() enables or disables the OnSubscriptionStatusChanged event, which fires whenever the overall PubNub subscription state changes (for example, connected, reconnected, or access denied).

Method signature​

1chat.StreamSubscriptionStatus(bool stream)

Event signature​

1// event on the Chat object — enabled by StreamSubscriptionStatus()
2public event Action<PNStatus> OnSubscriptionStatusChanged;
3// needs a corresponding event handler
4void EventHandler(PNStatus status)

Input​

ParameterRequired in StreamSubscriptionStatus()Required in OnSubscriptionStatusChangedDescription
stream
Type: bool
Default:
n/a
Yesn/aWhether to start (true) or stop (false) receiving subscription status events.
status
Type: PNStatus
Default:
n/a
NoYesThe PubNub status object containing the event category (for example, PNConnectedCategory, PNReconnectedCategory) and a list of affected channels.

Output​

This method doesn't return a value. Status changes are delivered through the OnSubscriptionStatusChanged event handler.

Sample code​

1

Was this page useful?

Last updated on