Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kevinlang/plug_socket
Add Cowboy websockets to your Plug.Router easily.
https://github.com/kevinlang/plug_socket
cowboy plug sockets websocket websockets
Last synced: 3 months ago
JSON representation
Add Cowboy websockets to your Plug.Router easily.
- Host: GitHub
- URL: https://github.com/kevinlang/plug_socket
- Owner: kevinlang
- License: apache-2.0
- Created: 2021-08-25T22:29:42.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-08-25T22:36:30.000Z (over 3 years ago)
- Last Synced: 2024-09-17T03:35:50.782Z (4 months ago)
- Topics: cowboy, plug, sockets, websocket, websockets
- Language: Elixir
- Homepage: https://hexdocs.pm/plug_socket
- Size: 8.79 KB
- Stars: 15
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PlugSocket
[Online Documentation](https://hexdocs.pm/plug_socket).
Enables a convenient way of specifying sockets within your `Plug.Router`.
`PlugSocket` is not a `Plug` itself. Instead, it provides a convenient
DSL for specifying any sockets as part of your `Plug.Router` that can
later be added to the `Plug.Cowboy` adapter.Because it is not tied to `Plug` or `Plug.Router` directly, it can also be
used independently of Plug, if one wants to just use `Cowboy` directly.## Usage
Add the `PlugSocket` to your router. E.g.,
defmodule MyApp.Router do
use Plug.Routersocket "/my-socket", MyApp.MySocket
plug :match
plug :dispatchget "/" do
send_resp(conn, 200, "hello world")
end
endEach `module` you pass to `socket/3` must implement the `:cowboy_websocket_handler`
behavior. Note that because `socket/3` is _not_ a `Plug`, it is not part of any
plug pipeline you create in your router.Next, you need to ensure our websockets are added to the `Cowboy` dispatch:
def start(_type, _args) do
children = [
{Plug.Cowboy, scheme: :http, plug: MyApp.Router, options: [
dispatch: PlugSocket.plug_cowboy_dispatch(MyApp.Router)
]}
]opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
endThis registers your sockets with the Cowboy dispatcher. You can now
start the application and navigate to your socket path in your client
and see that it is now routing!