Event listeners overview

Showing JavaScript examples.

An event listener is a callback your code registers to receive one specific kind of event PubNub delivers to a subscription. PubNub generates an event whenever something meaningful happens on a channel your client is subscribed to, such as a message being published, a signal being sent, or a user's presence changing. The listener is how that event reaches your application code. This page explains:

  • what a listener is, and what makes it run
  • why there is one handler per event type instead of one generic callback
  • the two ways SDKs let you register handlers
  • how listener scope follows what the listener is attached to
  • how a listener's lifecycle differs from the subscription it is attached to

One rule holds throughout. A listener only ever receives what its subscription receives, so nothing on this page substitutes for subscribing to the right channels in the first place. For the procedure that creates a subscription and registers listeners on it, refer to Receive messages.

A listener is a callback, not a connection​

A listener does not open anything and does not fetch anything on its own. It is a function your code hands to the SDK, and the SDK calls it when a matching event arrives on whatever the listener is attached to, a subscription or a subscription set.

Because a listener is only a callback, attaching one does nothing by itself. A subscription with every handler registered still receives nothing until it is started, so registering handlers and calling subscribe() are two separate steps. The order between them does not matter as long as both happen. A handler registered before or after the subscription starts fires identically once it is running. For that procedure, refer to Receive messages.

One handler per event type​

PubNub routes each event type to its own dedicated handler rather than delivering a single generic callback. The handlers are named consistently across entity-based SDKs: onMessage, onSignal, onPresence, onObjects, onMessageAction, and onFile.

Splitting by handler instead of by field means your code never inspects a payload to work out what kind of event it is looking at. The routing already happened before the callback ran. A chat client that treats a text message differently from a typing signal writes that distinction once, as two separate handlers that each only ever receive one kind of event. That beats a branch inside a single handler for everything.

For the payload each handler receives and the conditions each event type depends on, such as an add-on that must be enabled on your keyset, refer to Events.

The status handler is the exception​

Connection status is not an event on a channel. It reports the state of the subscribe connection, which is shared by every subscription and subscription set the client holds. For that reason the status handler is registered on the PubNub client object, never on an individual subscription, even in SDKs where every other handler is entity-based. Refer to Connection status events for the categories it reports and to The status listener for what to do with each one.

Two ways to register a handler​

Most SDKs offer the same handlers through two different call shapes. One is a dedicated property or setter per event type. The other is a single call that registers a generic listener covering several event types at once. Both register the same callback on the same subscription, and the difference is ergonomics, not behavior. A few SDKs expose only one shape. The language has no entity model to attach a per-event property to, and no generic listener object either, such as Go's single channel-based Listener. Those tabs below show the one mechanism that exists rather than a fabricated second one.

1

subscription1.addListener({ message, presence }) registers a generic listener covering several event types at once. subscriptionSet1.onMessageAction is the dedicated-property style, and it works the same way on a plain subscription.

Where an SDK offers both styles, the dedicated-property style suits handlers you assign or replace individually, since setting the property again replaces the previous callback. The generic-listener style suits registering a full set of handlers in one call. Neither style changes what a handler receives or when it fires. For the setup steps around these calls, refer to Receive messages.

Listener scope follows the subscription, not the channel name​

A subscription covers one entity, and a subscription set covers several as one unit. A handler registered on a subscription fires only for events on the entity that subscription was built from. A handler registered on a subscription set fires for events from every member of that set. Two separate subscriptions to the same channel run their own handlers independently, so removing one does not touch the other.

SDKs written before entities existed skip this scoping entirely. They register every handler on the PubNub client object with addListener(). Each one then fires for every channel and channel group that client is currently subscribed to, with no per-channel separation available. The event types and the fields each payload carries are identical either way, and only the registration mechanism and the resulting scope differ. Not every SDK supports entities yet, so check the API reference for your platform before assuming per-subscription scoping is available. Refer to SDK entities.

A listener's lifecycle is independent of its subscription's​

Registering a handler and starting a subscription are separate operations, and so are removing a handler and stopping one. A handler stays registered, and keeps firing, until your code removes it explicitly. Restarting or reconfiguring the subscription it is attached to does not clear it.

The reverse also holds. Removing a handler stops your code from hearing about that event type, but it does not unsubscribe anything. A subscription with every handler removed still receives events. It just has nothing registered to call when one arrives. Treat "stop handling an event" and "stop receiving a channel" as two independent decisions, and make both if you mean to fully stop. For the calls that do each one, refer to Stop receiving messages.

Next steps​

  • Receive messages. Create a subscription and register handlers on it.
  • Events. Every event type, what triggers it, and the fields its payload carries.
  • Subscribe. Subscriptions, subscription sets, and what a client is eligible to receive.
  • Subscriptions. How a subscription and a subscription set are built and scoped.
  • Stop receiving messages. Remove a handler and unsubscribe.
  • Connection management. The status listener, reconnection policies, and recovery.

Was this page useful?

Last updated on