kevinlang

kevinlang

PlugSocket - easily add Cowboy websockets to your Plug.Router

Hey all,

I created a library that allows you to easily add Cowboy websockets to your Plug.Router.

Check out the docs!

Check out the code!

Example

Add the PlugSocket to your router. Then use the socket/2 macro as necessary. E.g.,

defmodule MyApp.Router do
  use Plug.Router
  use PlugSocket

  socket "/my-socket", MyApp.MySocket

  plug :match
  plug :dispatch

  get "/" do
    send_resp(conn, 200, "hello world")
  end
end

Each 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. It is a “DSL” onto itself.

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)
end

This 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!


That’s all - the library is actually REALLY simple, I think no more than 20 lines of code. But the problem has come up time and again so I figured might as well make it a package.

Technically this library does not use or leverage Plug at all per se. But I couldn’t think of a better name, since most of the time people are looking for “how to use sockets with plug” I figure I may as well call it PlugSocket.

Cheers,
Kevin

First Post!

CombinedCognitions

CombinedCognitions

oh this is great
thank you for your effort :relaxed:

Where Next?

Popular in Libraries Top

woutdp
Hi! I wanted to introduce my latest project LiveSvelte. It allows you to render Svelte inside LiveView with end-to-end reactivity. It’s ...
New
benlime
LiveMotion enables high performance animations declared on the server and run on the client. As a follow up to my previous thread A libr...
New
Qqwy
Hello everyone, I wrote a small library today called MapDiff. It returns a map listing the (smallest amount of) changes to get from map...
New
zorbash
I created Kitto a framework for dashboards inspired by Dashing. [demo] The distributed characteristics of Elixir and the low memory foo...
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
wmnnd
Hi there, for my project DBLSQD, I needed a file storage solution that is a bit more flexible than Arc. Because I thought others might f...
New
engineeringdept
I’ve just released the first version of Snap, an Elasticsearch client. It borrows ideas about application structure and process managemen...
New
bryanjos
Hi, I wanted share a small library we at Revelry Labs made for rendering react components from the server side. There are instructions fo...
New
mischov
import Meeseeks.CSS html = HTTPoison.get!("https://news.ycombinator.com/").body for story <- Meeseeks.all(html, css("tr.athing")) do...
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Sub Categories:

We're in Beta

About us Mission Statement