Перейти к содержанию

Interface: TransportDriver

Abstract Transport Driver API

Describes anything capable of sending messages and subscribing to incoming messages (e.g., udp socket). Despite some complications due to not tracking message delivery, this is the most simple approach to bidirectional message transfer, which may be implemented via any kind of socket, http+sse etc.

Example

// Server-side
async (transport: TransportDriver) => {
  const subscription = transport.on({ room: '323-123-45' }, ({ topic }, data) => {
    expect(topic).toBe('hello');
    expect(data).toStrictEqual({ woo: 'hoo' });
  });

  // implementation

  transport.off(subscription);
};

// Client side
async (transport: TransportDriver) => {
  await transport.send({ room: '323-123-45', topic: 'hello' }, { woo: 'hoo' });
};

Methods

off

off(id, data?): void

Cancel an existing subscription

Does not throw if there are no subscriptions with this id. If current subscriber is actively awaiting a message, one may use the optional data field to resolve that.

Parameters

Name Type Description
id string subscription identifier returned upon creation
data? unknown (optional) message data to be sent to a current subscriber's callback

Returns

void


on

on(filter, callback): string

Subscribe to specific kinds of messages

It is possible to subscribe to the same message twice, which may be used in advanced message filtering strategies.

Parameters

Name Type Description
filter Partial<TransportMeta> incoming message filter
callback TransportCallback callback to be invoked with messages matching a provided filter

Returns

string

subscription identifier, used to cancel this subscription


send

send(meta, data): Promise<void>

Send a message via transport

This method awaits until the message is sent (not until it's delivered) and throws if sending is impossible. A specific destination and delivery are handled by implementation.

Parameters

Name Type Description
meta TransportMeta message identifiers
data unknown message data

Returns

Promise<void>