T2 Bus
A message bus supporting publish/subscribe and request/response.
The bus supports three transport types:
- Memory: messages are passed via a memory queue between clients that exist within the same process.
- Unix: Messages are passed via a Unix socket between clients that exist within the same or different processes on a single host machine.
- TCP: Messages are passed via a TCP socket between clients that may be on different hosts.
Internally, messages are serialized using the CBOR format and compressed using GZIP if above a certain size threshold.
Usage
Start a bus server
use *;
async
Alternatively, if you don't need an in-process bus, you may use the provided command line utility t2 to start a TCP and/or Unix socket server:
Connect to a server
use *;
async
Publish/Subscribe
Clients may subscribe to topics in order to receive all messages published on matching topics.
use ;
use *;
// Define a protocol message type
;
// Specify that this is a pub/sub protocol
// ..
async
Request/Response
Clients may begin to "serve" a topic. While serving a topic, requests on that protocol and topic will be routed exclusively to that client. The serving client should then produce a response which will be routed back to the origin of the request.
At most one client may serve a given topic at a time. However, it is allowed to publish/subscribe on a topic while that topic is being served as the two systems do not interact.
Topic wildcards (see below) are not supported for either serving or requesting.
use *;
use ;
// Define protocol message types for request and response
;
;
// Specify that this is a req/rsp protocol
async
Topic Matching
Messages are routed according to topics. A topic is a string similar in form to a file system path. Here are some examples:
price
price/eth
price/eth/eur
price/*/eur
price/**
The topic is composed of "fragments" separated by /. Each fragment is either a word composed of a-z and _ or the wildcard * or the double wildcard **.
Word fragments match identical word fragments. The wildcard * matches any fragment at the same position. The double wildcard ** matches any fragment any number of times.
Wildcards are supported in both subscribe topics and publish topics.
To illustrate topic matching, consider an example chat app. If we define that messages from a user in a room are published to the topic <room>/<user> then:
alicepublishes a message in thelobbyby publishing tolobby/alice.alicepublishes a message in all rooms by publishing to*/alicebobsubscribes toalice's messages in thelobbyby subscribing tolobby/alicebobsubscribes to all messages in thelobbyby subscribing tolobby/*bobsubscribes to alice's messages in all rooms by subscribing to*/alicebobsubscribes to all messages in all rooms by subscribing to**