To run in the console, without supervision trees, do the following:
$ iex -S mix
iex> Plug.Adapters.Cowboy2.http(NormalRouter, [], [dispatch: PlugDispatch.dispatch()])
To run inside a supervision tree, make sure to call child_spec
(or use a tuple) with the correct arguments, like in the following example:
children = [
{Plug.Adapters.Cowboy2, scheme: :http, plug: NormalRouter, options: [dispatch: PlugDispatch.dispatch()]}
]
- Open http://localhost:4000/ to see
NormalRouter
in action. - Make a request to http://localhost:4000/sse to see
SseHandler
in action. Send OTP message to the connection process (The PID is printed atinit
). - Remember you can get PID from its numbers by using
:c.pid/3
. for example::c.pid(0,123,0)
, assuming the PID is "0.123.0" - Example OTP message:
send :c.pid(0,379,0), {:event, "This is an OTP event!!!\n" }
- By default, idle connections are closed after 60s, and process gets killed
This handler was partially inspired by the gist: https://gist.github.com/alanpeabody/4fae12b420fb50376af4