Skip to content

Commit

Permalink
sending messages to the browser
Browse files Browse the repository at this point in the history
  • Loading branch information
CrowdHailer committed May 28, 2018
1 parent d36671f commit a50bf80
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/watercooler/www.ex
Expand Up @@ -4,6 +4,7 @@ defmodule Watercooler.WWW do
use Raxx.Router, [ use Raxx.Router, [
{%{method: :GET, path: []}, Watercooler.WWW.HomePage}, {%{method: :GET, path: []}, Watercooler.WWW.HomePage},
{%{method: :POST, path: ["publish"]}, Watercooler.WWW.Publish}, {%{method: :POST, path: ["publish"]}, Watercooler.WWW.Publish},
{%{method: :GET, path: ["listen"]}, Watercooler.WWW.Listen},
{_, Watercooler.WWW.NotFoundPage} {_, Watercooler.WWW.NotFoundPage}
] ]


Expand Down
21 changes: 21 additions & 0 deletions lib/watercooler/www/listen.ex
@@ -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
1 change: 1 addition & 0 deletions mix.exs
Expand Up @@ -20,6 +20,7 @@ defmodule Watercooler.Mixfile do
{:ace, "~> 0.16.4"}, {:ace, "~> 0.16.4"},
{:phoenix_html, "~> 2.11"}, {:phoenix_html, "~> 2.11"},
{:raxx_static, "~> 0.6.1"}, {:raxx_static, "~> 0.6.1"},
{:server_sent_event, "~> 0.3.1"},
{:exsync, "~> 0.2.3", only: :dev}, {:exsync, "~> 0.2.3", only: :dev},
] ]
end end
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Expand Up @@ -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"}, "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": {: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"}, "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.