This version (v0.7) has not been published to npm yet.
Skip to content

Reference / @getenginex/sdk/extension / defineTopic

defineTopic() ​

Call Signature ​

defineTopic<TWire, TMessage>(definition): () => Topic<TMessage>

Defines a realtime topic and returns the factory of its descriptors: with no arguments on a private topic (the id is the session's sub), with the id on a public one. Meant for SDK packages, not for apps: an app uses the ready-made topics (coreTopics, predictionsTopics).

Type Parameters ​

TWire ​

TWire

TMessage ​

TMessage

Parameters ​

definition ​

TopicDefinition<TWire, TMessage> & object

Prefix, privacy, message type and payload schema.

Returns ​

() => Topic<TMessage>

Example ​

ts
// defineTopic, lazy and wire come from @getenginex/sdk/extension
const marketSchema = lazy(() => wire({ market_id: z.string(), status: z.string() }))
const market = defineTopic({ prefix: 'market', private: false, messageType: 'market_update', schema: marketSchema })
realtime.subscribe(market('m-1'), (message) => console.log(message.marketId))

Throws ​

CONFIGURATION_ERROR on the definition (empty prefix, with a trailing : or outside the gateway format; empty messageType; a schema that is neither a Zod schema nor a function) and, on creating the descriptor (id passed to a private topic; on a public one, an empty id or a full topic outside ^[a-zA-Z0-9_:.-]+$ with 2–256 characters). Identify it with isEnginexError(error, 'CONFIGURATION_ERROR').

Call Signature ​

defineTopic<TWire, TMessage>(definition): (id) => Topic<TMessage>

Defines a public topic: the factory requires the id (e.g. the market id).

Type Parameters ​

TWire ​

TWire

TMessage ​

TMessage

Parameters ​

definition ​

TopicDefinition<TWire, TMessage> & object

Prefix, privacy, message type and payload schema.

Returns ​

(id) => Topic<TMessage>

Example ​

ts
const orderbook = defineTopic({
  prefix: 'predictions:orderbook',
  private: false,
  messageType: 'orderbook_update',
  schema: orderbookSchema,
})
realtime.subscribe(orderbook('m-1'), (book, meta) => apply(book, meta.kind))

Throws ​

CONFIGURATION_ERROR, as in the private topic subscription.