Elixir MOM v0.5.3 MOM.Channel.Broadcast

Broadcast channel. All messages are sent to all clients.

Summary

Functions

Handles sync send of messages, including :deadletter and :invalid message management

Handles normal send of messages, including :deadletter and :invalid message management

state is just the subscriber list callbacks, it will call each function with the message

Send a message to a channel

Starts the link

Subscribes to a channel

Unsubscribes to a channel

Functions

handle_call(msg, arg2, state)

Handles sync send of messages, including :deadletter and :invalid message management.

In some situations sowrk should not continue until the message has been delivered to all points, for example at authentication, where following work may require this message to have been processed.

handle_cast(msg, state)

Handles normal send of messages, including :deadletter and :invalid message management.

init(args)

state is just the subscriber list callbacks, it will call each function with the message.

send(channel, message, options, timeout)

Send a message to a channel.

Always returns :ok and it is asynchronous.

The way to know if it was sucesfull is listen to the :invalid channel.

Options

  • sync — Default false. Wait untill all messages processed.

Examples

Depending on how succesful was the send it returns different values:

iex> alias MOM.{Channel, Message}
iex> {:ok, ch} = Channel.Broadcast.start_link
iex> Channel.send(ch, %Message{})
:ok
iex> Channel.subscribe(ch, fn _ -> :ok end)
iex> Channel.send(ch, %Message{})
:ok
iex> Channel.subscribe(ch, fn _ -> raise "To return :nok" end)
iex> Channel.send(ch, %Message{}, sync: true)
:ok

Channels can self-unsubscribe returning :unsubscribe from the called function.

iex> alias MOM.{Channel, Message}
iex> {:ok, a} = Channel.Broadcast.start_link
iex> {:ok, data} = Agent.start_link(fn -> 0 end)
iex> Channel.subscribe(a, fn _ ->
...>   Logger.info("Called")
...>   Agent.update(data, &(&1 + 1))
...>  :unsubscribe
...> end)
iex> Channel.send(a, %Message{})
:ok
iex> Channel.send(a, %Message{})
:ok
iex> :timer.sleep(100) #send is async, wait for it
iex> Agent.get(data, &(&1))
1
start_link()

Starts the link

stop(pid)
stop(pid, reason)
subscribe(channel, subscriber, options)

Subscribes to a channel.

unsubscribe(channel, subscriber)

Unsubscribes to a channel