Messaging patterns—designing reliable conversations

Make messages traceable, cancellable, and versioned across SW, content scripts, and UI.

Patterns

  • One-off for simple RPC.
  • Ports for streams, long operations, or bi-directional updates.
  • Pub/Sub for fan-out via a hub in the service worker.

Contracts & errors

Use a stable envelope with type, requestId, version, payload, and error. Add timeouts, bounded retries, and idempotency keys for at-least-once delivery.

{
  "type": "CATALOG/UPDATE_PRICE",
  "requestId": "req_9t7f",
  "version": 2,
  "payload": { "sku": "A123", "price": 19.99 },
  "error": null
}

Backpressure

  • Queue, drop, or coalesce—never allow unbounded buffers.
  • Set concurrency caps and apply jittered delays for fairness.

Versioning

  • Include a protocol version in each envelope.
  • Use capability checks to deprecate safely.