Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@josevalim
Forked from rhalukongor/example.ex
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josevalim/45898f5468dfec05de04 to your computer and use it in GitHub Desktop.
Save josevalim/45898f5468dfec05de04 to your computer and use it in GitHub Desktop.
defmodule SomeModule do
def start(host, options) do
# We are assuming SomeSup has been already started somewhere
# (ideally in a supervision tree).
{:ok, child_pid} = SomeSup.attach_to(SomeSup, host, options)
end
end
defmodule SomeSup do
use Supervisor
def attach_to sup, host, options do
Supervisor.start_child(sup, [host, options])
end
def start_link do
Supervisor.start_link(__MODULE__, [], name: __MODULE__)
end
def init [] do
supervise([worker(SomeWorker, [], restart: :temporary)], strategy: :simple_one_for_one)
end
end
defmodule SomeWorker do
use GenServer
alias :gproc, as: GProc
def start_link host, options do
GenServer.start_link(__MODULE__, [host, options], name: {:via, GProc, {:n, :l, :conv_id}})
end
def init [host, options] do
#Let GProc know that this is a conversation
GProc.reg({:p, :l, :conversation})
{:ok, []}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment