Message pkt
are dispatched accompanied with the delivery pktctx
for access to the source channel and routers.
Use addStream()
to implement stateful or async serial message processing endpoints.
Use addTarget()
to implement stateless or async independently parallel message processing endpoints.
pkt
is a JSON-like object with that is transferred through structured clone algorithm, or using codecs like JSON or CBOR.
pkt[0]
isid_router
stringpkt[1]
isid_target
stringpkt.body
is the payload, and may be an{}
object,[]
list, or""
string.pkt.meta
is optional, and may be an{}
object,[]
list, or""
string.
pktctx
is a prototypal object extension of hub.router.router_ctx
:
channel
is the source channelpkt
arrived fromchannel.send(...args)
to bypass generic routing overhead.redispatch(pkt, pktctx)
is useful when routes are discovered while handling packet
Extending router_ctx
:
send(...args)
-- alias forloopback.send(...args)
loopback
-- alias forhub.router.loopback
timeouts
-- returns shared timer. See timeouts docshub_router
-- reference tohub.router
tgt_router
-- reference to target router hosting the target endpoint
An event based target implementation for parallel message packet processing.
Easiest to use when your target is stateless; for stateful endpoints see addStream()
.
// tgt_addr is [id_router, id_target]
let tgt_addr = hub.local.addTarget(
id_target, // or null for an assigned random id
(pkt, pktctx) => {
// ... handle message pkt
})
A stream-based target implementation for serial async message packet processing.
Easier stateful target implementation; for stateless endpoints see addTarget()
.
let xtgt_info = hub.local.addStream(
id_target, // or null for an assigned random id
async (xtgt) => {
// xtgt_info === xtgt
for await (let [pkt, pktctx] of xtgt.stream) {
// ... handle message pkt
}
})
xtgt.id
is[id_router, id_target]
xtgt[0]
isid_router
xtgt[1]
isid_target
xtgt.stream
is an async iterable of[pkt, pktctx]
messagesxtgt.abort()
aborts the async iterable message streamxtgt.when_done : promise
is resolved whenasync_target
callback completes or errors.
A promise-based response channel mechanism.
let reply = hub.local.addOnce()
hub.send(tgt_addr,
{ msg: 'hello readme example with reply',
id_reply: reply.id
})
let ans = await reply.promise
console.log('Received reply', ans)