-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.exs
34 lines (28 loc) · 893 Bytes
/
publish.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Mix.install([:httpoison, :jason])
defmodule EventPublisher do
def publish(url, channel, data) do
body = Jason.encode!(%{"data" => data})
headers = [
{"Content-Type", "application/json"},
{"Authorization",
"Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxfQ.rINrV4jad74K9b1W39TEKlqXpG63h-dn-yfqQpVEztuhomwW4lZ36j6cKl9IXLiq43zvmNjBlMOA_aCbgofQOg"}
]
HTTPoison.post("#{url}/publish/#{channel}", body, headers)
end
end
url = "http://localhost:4000"
channel = "order.us.deliver"
data = %{
"message" => "Hello, SSE! Your order is delivered!",
"user_id" => 1,
"item_title" => "Elixir in Action",
"item_type" => "book",
"item_id" => 1,
"quantity" => 1,
"total" => 10.0,
"currency" => "USD"
}
case EventPublisher.publish(url, channel, data) do
{:ok, response} -> IO.inspect(response)
{:error, error} -> IO.inspect(error)
end