Skip to content

SharedWorker - Cannot destructure property 'resolve' of 'this.callbacks.get(...)' as it is undefined. #489

@sonu-tz

Description

@sonu-tz

Issue: SharedWorker callbacks become unavailable after tabs go idle (multi-tab scenario)

Description

When using a SharedWorker to share a single PubNub connection across multiple browser tabs, we are encountering an issue where callbacks (MessagePort communication) become unavailable after tabs remain idle or backgrounded for some time.

This happens especially when:

  • Multiple tabs are connected to the same SharedWorker
  • Tabs go idle / backgrounded for a while
  • A PubNub event is received after the idle period

At that point, the SharedWorker attempts to notify all connected tabs, but some ports throw errors indicating that the callback or message port is no longer available.


Expected Behavior

  • SharedWorker should be able to safely broadcast PubNub events to all active tabs.
  • Inactive or backgrounded tabs should not cause errors when PubNub events arrive.
  • Message ports that are no longer valid should be safely ignored or cleaned up.

Actual Behavior

  • After tabs remain idle/backgrounded, PubNub events trigger errors such as:
    • callback not available
    • failed postMessage / port is closed
  • This breaks event delivery and sometimes impacts other active tabs.

Environment

  • Angular - v20.2
  • Pubnub - 9.7.0 & same version worker file
  • Browser: Chrome / Chromium-based browsers

Steps to Reproduce

  1. Open multiple tabs of the same application
  2. All tabs connect to a SharedWorker
  3. SharedWorker initializes a single PubNub connection
  4. Leave tabs idle or backgrounded for some time
  5. Trigger a PubNub message/event
  6. Observe errors when the SharedWorker attempts to postMessage to inactive tabs

Questions

  1. Are there recommended best practices from PubNub for using the JS SDK inside a SharedWorker?
  2. Is there an officially supported pattern for multi-tab PubNub usage (SharedWorker vs ServiceWorker vs per-tab connection)?
  3. Are there SDK-level safeguards or lifecycle hooks that can help detect inactive consumers?
  4. Is there any special benefits of using or we can just remove that completely?

Possible Workarounds (Current)

  • after removing SharedWorker it works fine, // subscriptionWorkerUrl: "/pubnub.worker.9.7.0.min.js"

Any guidance or examples from the PubNub team would be greatly appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions