ShardBus
Defined in: packages/sharding/src/bus.ts:76
Cross-shard messaging: send, broadcast, and request/reply over a ShardBusTransport.
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new ShardBus(
shardId,channelName,options?):ShardBus
Defined in: packages/sharding/src/bus.ts:110
Creates a shard bus.
Parameters
Section titled “Parameters”shardId
Section titled “shardId”number
Shard ID.
channelName
Section titled “channelName”string
Application-specific channel name.
options?
Section titled “options?”ShardBusOptions = {}
Returns
Section titled “Returns”ShardBus
Throws
Section titled “Throws”If shard ID is invalid.
Throws
Section titled “Throws”If channel name is empty.
Properties
Section titled “Properties”channelName
Section titled “channelName”
readonlychannelName:string
Defined in: packages/sharding/src/bus.ts:92
BroadcastChannel name used by this bus.
Methods
Section titled “Methods”broadcast()
Section titled “broadcast()”broadcast<
T>(type,data):string
Defined in: packages/sharding/src/bus.ts:177
Broadcasts to all other shards.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”string
Message type.
T
Payload.
Returns
Section titled “Returns”string
Unique message ID.
broadcastRequest()
Section titled “broadcastRequest()”broadcastRequest<
R,T>(type,data,options?):Promise<ShardReply<R>[]>
Defined in: packages/sharding/src/bus.ts:241
Sends a request to every other shard and collects replies until
expected have arrived or timeoutMs elapses. A timeout or a shard
going away does not reject; a closed bus, a blank type or too many
pending requests does.
Type Parameters
Section titled “Type Parameters”R = unknown
T = unknown
Parameters
Section titled “Parameters”string
T
options?
Section titled “options?”expected?
Section titled “expected?”number
timeoutMs?
Section titled “timeoutMs?”number
Returns
Section titled “Returns”Promise<ShardReply<R>[]>
close()
Section titled “close()”close():
void
Defined in: packages/sharding/src/bus.ts:300
Closes the transport.
Returns
Section titled “Returns”void
Nothing.
off<
T>(type,handler):this
Defined in: packages/sharding/src/bus.ts:140
Removes a message handler.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”string
Message type.
handler
Section titled “handler”ShardMessageHandler<T> | ((data, message) => unknown)
Handler callback.
Returns
Section titled “Returns”this
This bus.
offError()
Section titled “offError()”offError(
handler):this
Defined in: packages/sharding/src/bus.ts:164
Removes a handler error listener.
Parameters
Section titled “Parameters”handler
Section titled “handler”Error listener.
Returns
Section titled “Returns”this
This bus.
on<
T>(type,handler):this
Defined in: packages/sharding/src/bus.ts:131
Registers a message handler.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”string
Message type.
handler
Section titled “handler”Handler callback.
Returns
Section titled “Returns”this
This bus.
Throws
Section titled “Throws”If type or handler is invalid.
onError()
Section titled “onError()”onError(
handler):this
Defined in: packages/sharding/src/bus.ts:157
Registers a listener for errors thrown or rejected by message handlers. Without one, handler errors are isolated and dropped.
Parameters
Section titled “Parameters”handler
Section titled “handler”Error listener.
Returns
Section titled “Returns”this
This bus.
Throws
Section titled “Throws”If handler is invalid.
request()
Section titled “request()”request<
R,T>(target,type,data,timeoutMs?):Promise<R>
Defined in: packages/sharding/src/bus.ts:210
Sends a request to one shard and resolves with its handler’s result.
Type Parameters
Section titled “Type Parameters”R = unknown
T = unknown
Parameters
Section titled “Parameters”target
Section titled “target”number
string
T
timeoutMs?
Section titled “timeoutMs?”number = 5000
Returns
Section titled “Returns”Promise<R>
Throws
Section titled “Throws”If the handler failed or no reply arrives in time.
respond()
Section titled “respond()”respond<
T,R>(type,handler):this
Defined in: packages/sharding/src/bus.ts:186
Registers a handler that answers requests of type. Its return value (or
thrown error) is sent back to the requesting shard. Received code is
never evaluated.
Type Parameters
Section titled “Type Parameters”T
R
Parameters
Section titled “Parameters”string
handler
Section titled “handler”(data, message) => R | Promise<R>
Returns
Section titled “Returns”this
This bus.
send()
Section titled “send()”send<
T>(target,type,data):string
Defined in: packages/sharding/src/bus.ts:169
Sends a targeted shard message.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”target
Section titled “target”number
Target shard ID.
string
Message type.
T
Payload.
Returns
Section titled “Returns”string
Unique message ID.