Permalink
Browse files

sending messages to the browser

  • Loading branch information...
CrowdHailer committed May 28, 2018
1 parent d36671f commit a50bf802cac5503f1c7665c828fdb266a3f348bc
Showing with 24 additions and 0 deletions.
  1. +1 −0 lib/watercooler/www.ex
  2. +21 −0 lib/watercooler/www/listen.ex
  3. +1 −0 mix.exs
  4. +1 −0 mix.lock
View
@@ -4,6 +4,7 @@ defmodule Watercooler.WWW do
use Raxx.Router, [
{%{method: :GET, path: []}, Watercooler.WWW.HomePage},
{%{method: :POST, path: ["publish"]}, Watercooler.WWW.Publish},
{%{method: :GET, path: ["listen"]}, Watercooler.WWW.Listen},
{_, Watercooler.WWW.NotFoundPage}
]
@@ -0,0 +1,21 @@
defmodule Watercooler.WWW.Listen do
use Raxx.Server
alias ServerSentEvent, as: SSE
@impl Raxx.Server
def handle_request(_request, state) do
{:ok, _} = Watercooler.listen() # 1.
response = response(:ok)
|> set_header("content-type", SSE.mime_type())
|> set_body(true) # 2.
{[response], state} # 3.
end
@impl Raxx.Server
def handle_info({:watercooler, message}, state) do
event = SSE.serialize(message)
{[Raxx.data(event)], state} # 4.
end
end
View
@@ -20,6 +20,7 @@ defmodule Watercooler.Mixfile do
{:ace, "~> 0.16.4"},
{:phoenix_html, "~> 2.11"},
{:raxx_static, "~> 0.6.1"},
{:server_sent_event, "~> 0.3.1"},
{:exsync, "~> 0.2.3", only: :dev},
]
end
View
@@ -9,4 +9,5 @@
"plug": {:hex, :plug, "1.5.1", "1ff35bdecfb616f1a2b1c935ab5e4c47303f866cb929d2a76f0541e553a58165", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1 or ~> 2.3", [hex: :cowboy, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm"},
"raxx": {:hex, :raxx, "0.15.4", "62e4a487e55e9469d1ba2d5de590672167f51eaac310f027f3f871018cb6a9fc", [:mix], [{:cookie, "~> 0.1.0", [hex: :cookie, repo: "hexpm", optional: false]}], "hexpm"},
"raxx_static": {:hex, :raxx_static, "0.6.1", "8b48254fc3d1b8b1e473b7c307fbba0ae767c60482754ce823c664544c85d729", [:mix], [{:mime, "~> 1.1", [hex: :mime, repo: "hexpm", optional: false]}, {:raxx, "~> 0.15.2", [hex: :raxx, repo: "hexpm", optional: false]}], "hexpm"},
"server_sent_event": {:hex, :server_sent_event, "0.3.1", "4126788362090bfd542be90f65571c2b0155c7e4032bf557a4f91a34a563acc1", [:mix], [], "hexpm"},
}

0 comments on commit a50bf80

Please sign in to comment.