PubSub Multi
Multiple implementations of Publish-Subscribe.
- In-memory
- HTTP EventSource and POST as transport
Both implementations share the same contract tests, allowing them to be used interchangeably.
The API for publishing is simple:
// publisher is an instance of MemoryPublisherawait publisher
The API for publishing too:
// publisher is an instance of MemoryPublisher or EventSourcePubSubconst subscriber = publisherawait subscriber
HTTP
To use this over HTTP, mount the express middleware in your express app:
const express = const bodyParser = const MemoryPublisher pubSubRouter = const publisher = // Implements both pub and sub interface const app = appappapp
To publish, you would use publisher.publish
as described above. The pubSubRouter
then sends signals to
subscribers using Server-Sent Events as the transport.
Clients are configured as follows:
const EventSourcePubSub = const Fetch22 = // Tiny lib that simplifies HTTPconst fetch = windowfetchconst baseUrl = 'http://host.com'const fetch22 = baseUrl fetch const eventSource = `/pubsub` const publisher = fetch22 eventSource const subscriber = publisher
To subscribe, you would use subscriber.subscribe
as described above.
The EventSourcePubSub
registers subscriptions on the server
with HTTP POST.