Skip to content
Permalink
Browse files

Broadcast telnet client updates over Phoenix PubSub

  • Loading branch information...
oestrich committed Mar 18, 2019
1 parent e8e0b1b commit 4757ca0be74c91b2be57213036167c856eeb1163
Showing with 12 additions and 1 deletion.
  1. +1 −0 config/config.exs
  2. +1 −0 lib/telnet/application.ex
  3. +8 −1 lib/telnet/presence.ex
  4. +2 −0 mix.exs
@@ -1,5 +1,6 @@
use Mix.Config

config :phoenix, :json_library, Jason
config :telnet, :errors, report: false

if File.exists?("config/#{Mix.env()}.exs") do
@@ -10,6 +10,7 @@ defmodule Telnet.Application do
children = [
cluster_supervisor(),
metrics_plug(),
{Phoenix.PubSub.PG2, [name: Grapevine.PubSub]},
{Telnet.ClientSupervisor, [name: {:global, Telnet.ClientSupervisor}]},
{Telnet.Presence, []},
{Telemetry.Poller, telemetry_opts()},
@@ -48,7 +48,7 @@ defmodule Telnet.Presence do
end

@doc """
Let the server know a web client came onlin
Let the server know a web client came online
"""
def client_online(opts) do
GenServer.cast({:global, __MODULE__}, {:client, :online, self(), opts, Timex.now()})
@@ -92,6 +92,8 @@ defmodule Telnet.Presence do

:ets.insert(@ets_key, {pid, game.id, open_client})

Phoenix.Channel.Server.broadcast(Grapevine.PubSub, "telnet:presence", "client/online", open_client)

{:noreply, Map.put(state, :clients, [pid | state.clients])}
end

@@ -103,13 +105,18 @@ defmodule Telnet.Presence do
open_client ->
open_client = %{open_client | player_name: player_name}
:ets.insert(@ets_key, {pid, open_client.game.id, open_client})
Phoenix.Channel.Server.broadcast(Grapevine.PubSub, "telnet:presence", "client/update", open_client)
{:noreply, state}
end
end

def handle_info({:EXIT, pid, _reason}, state) do
open_client = Implementation.fetch_from_ets(pid)
Phoenix.Channel.Server.broadcast(Grapevine.PubSub, "telnet:presence", "client/offline", open_client)

state = Map.put(state, :clients, List.delete(state.clients, pid))
:ets.delete(@ets_key, pid)

{:noreply, state}
end

@@ -27,6 +27,8 @@ defmodule Telnet.MixProject do
{:jason, "~> 1.1"},
{:libcluster, "~> 3.0"},
{:plug_cowboy, "~> 2.0"},
{:phoenix, "~> 1.4", override: true},
{:phoenix_pubsub, "~> 1.0"},
{:prometheus_ex, "~> 3.0"},
{:prometheus_plugs, "~> 1.1.1"},
{:sentry, "~> 7.0"},

0 comments on commit 4757ca0

Please sign in to comment.
You can’t perform that action at this time.