A simple in-process message/event broker for Clojure.
A simple in-process message/event broker for Clojure.
(publish! broker msg)
Publishes msg
to subscribers, who will be notified asynchronously.
msg
must not be nil
. Returns true
unless broker
is stopped.
Under high load, this will block the caller to respect back-pressure.
As such, this should not be called from (go ...)
blocks.
When blocking is not desired, use core.async/put!
on the channel
returned by publish-chan
, or install a dropping or sliding buffer
in the broker using the :buf-or-n
option of start
.
Publishes `msg` to subscribers, who will be notified asynchronously. `msg` must not be `nil`. Returns `true` unless `broker` is stopped. Under high load, this will block the caller to respect back-pressure. As such, this should not be called from `(go ...)` blocks. When blocking is not desired, use `core.async/put!` on the channel returned by [[publish-chan]], or install a dropping or sliding buffer in the broker using the `:buf-or-n` option of [[start]].
(publish-chan broker)
Returns the channel used for publishing messages to the broker.
Intended for publishing from a (go ...) block or advanced usage such as bulk publishing / piping into the broker.
Closing the channel will stop the broker.
Returns the channel used for publishing messages to the broker. Intended for publishing from a (go ...) block or advanced usage such as bulk publishing / piping into the broker. Closing the channel will stop the broker.
(start)
(start opts)
Starts a broker.
Supported options:
:topic-fn
- function used to determine the topic of an incoming message
for subscribe
; default: :key
.:xform
- a transducer to transform/filter messages as they are published.:buf-or-n
- async buffer or fixed buffer size to use for the publish
channel. Defaults to a 1024
.:buf-fn
- function to create async buffers for subscribing functions.
By default, uses small fixed-size buffers.:error-fn
- when a subscribing function throws an exception, this function
will be called with two arguments: the exception and a map with keys
:broker
, :fn
, and :msg
. With no :error-fn
(default), exceptions are
passed to the current thread's UncaughtExceptionHandler
.Starts a broker. Supported options: * `:topic-fn` - function used to determine the topic of an incoming message for [[subscribe]]; default: `:key`. * `:xform` - a transducer to transform/filter messages as they are published. * `:buf-or-n` - async buffer or fixed buffer size to use for the publish channel. Defaults to a `1024`. * `:buf-fn` - function to create async buffers for subscribing functions. By default, uses small fixed-size buffers. * `:error-fn` - when a subscribing function throws an exception, this function will be called with two arguments: the exception and a map with keys `:broker`, `:fn`, and `:msg`. With no `:error-fn` (default), exceptions are passed to the current thread's `UncaughtExceptionHandler`.
(stop! broker)
Stops the broker, closing all internal async channels.
After that, the broker will no longer accept messages. Any priorly
published messages will still be delivered. Returns nil
.
Stops the broker, closing all internal async channels. After that, the broker will no longer accept messages. Any priorly published messages will still be delivered. Returns `nil`.
(subscribe broker topic fn-or-ch)
Subscribes a function or channel to a topic.
If fn-or-ch
is already subscribed to this topic, this is a no-op.
When fn-or-ch
is a function, it will be run in an async (go ...)
block
and should not block. Exceptions will be reported to the broker's
:error-fn
(see start
).
Subscribes a function or channel to a topic. If `fn-or-ch` is already subscribed to this topic, this is a no-op. When `fn-or-ch` is a function, it will be run in an async `(go ...)` block and should not block. Exceptions will be reported to the broker's `:error-fn` (see [[start]]).
(subscribe-all broker fn-or-ch)
Subscribes a function or channel to all messages published to the broker.
When fn-or-ch
is already subscribed to all messages, this is a no-op.
When fn-or-ch
is also subscribed explicitly to a topic, it will receive
messages twice.
When fn-or-ch
is a function, it will be run in an async (go ...)
block
and should not block. Exceptions will be reported to the broker's
:error-fn
(see start
).
Subscribes a function or channel to all messages published to the broker. When `fn-or-ch` is already subscribed to all messages, this is a no-op. When `fn-or-ch` is also subscribed explicitly to a topic, it will receive messages twice. When `fn-or-ch` is a function, it will be run in an async `(go ...)` block and should not block. Exceptions will be reported to the broker's `:error-fn` (see [[start]]).
(thread-uncaught-exc-handler e _)
Passes exceptions to the current thread's UncaughtExceptionHandler
.
Passes exceptions to the current thread's `UncaughtExceptionHandler`.
(unsubscribe broker fn-or-ch)
(unsubscribe broker topic fn-or-ch)
Unsubscribes a function or channel.
If a topic
is given, unsubscribes only from this topic. Otherwise,
unsubscribes from all topics, i.e. clears all subscriptions from
subscribe
and subscribe-all
.
If fn-or-ch
is not a subscriber, this is a no-op. Returns nil
.
Unsubscribes a function or channel. If a `topic` is given, unsubscribes only from this topic. Otherwise, unsubscribes from all topics, i.e. clears all subscriptions from [[subscribe]] and [[subscribe-all]]. If `fn-or-ch` is not a subscriber, this is a no-op. Returns `nil`.
(unsubscribe-all broker)
(unsubscribe-all broker topic)
Unsubscribes all subscribers.
When a topic
is given, only subscribers to the given topic will be
unsubscribed. Returns nil
.
Unsubscribes all subscribers. When a `topic` is given, only subscribers to the given topic will be unsubscribed. Returns `nil`.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close