💧 Publish/Subscribe utility module
Switch branches/tags
Nothing to show
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config Initial commit Jun 15, 2015
lib Prevent duplicate subscriptions Oct 2, 2018
test Prevent duplicate subscriptions Oct 2, 2018
.gitignore Initial commit Jun 15, 2015
.travis.yml Add inch_ex Jul 17, 2017
README.md Add documentation badge in readme Jul 17, 2017
mix.exs Bump version: 1.1.0 → 1.1.1 Oct 2, 2018
mix.lock allow process names for clients Jan 18, 2018

README.md

Elixir Publish/Subscribe

Build Status Coverage Status Documentation Total Downloads

A Publish/Subscribe utility module that frees your business logic processes from the burden of communication.

Getting Started

Add :pubsub as a dependency to your mix.exs file:

defp deps do
  [
    {:pubsub, "~> 1.0"}
  ]
end

Then run mix deps.get in your shell to fetch the dependencies.

Examples

Assuming your client process looks like this:

defmodule Client do

  def start(client_name) do
    spawn(fn -> loop(client_name) end)
  end

  def loop(name) do
    receive do
      message ->
        IO.puts "#{name} received `#{message}`"
        loop(name)
    end
  end

end

With PubSub you can do this:

iex(1)> {topic1, topic2} = {:erlang, :elixir}
{:erlang, :elixir}

iex(2)> {:ok, pid} = PubSub.start_link()
{:ok, #PID<0.99.0>}

iex(3)> {pid1, pid2, pid3} =
...(3)> {
...(3)> Client.start("John"),
...(3)> Client.start("Nick"),
...(3)> Client.start("Tim")
...(3)> }
{#PID<0.106.0>, #PID<0.107.0>, #PID<0.108.0>}

iex(4)> PubSub.subscribe(pid1, topic1)
:ok
iex(5)> PubSub.subscribe(pid2, topic1)
:ok
iex(6)> PubSub.subscribe(pid3, topic2)
:ok

iex(7)> PubSub.publish(topic1, "#{topic1} is great!")
"Nick received `erlang is great!`"
"John received `erlang is great!`"
:ok

iex(8)> PubSub.publish(topic2, "#{topic2} is so cool, dude")
"Tim received `elixir is so cool, dude`"
:ok

API Reference

https://hexdocs.pm/pubsub/PubSub.html