Cowboy 2.9.0 reverse proxy websockets

Hey folks, I am a newer of elixir, but I recently got a urgent demand。It
need forward the websocket request from the cowboy sever ( to another server ( In short, how to implement reverse proxy websockets using Cowboy.
The following is the framework I used:
cowboy: 2.9.0
plug_cowboy: 2.6.0
plug: 1.14.0

The following is my simple code:

# application.ex
defmodule Example.Application do
  use Application
  require Logger

  def start(_type, _args) do
    children = [
      {Plug.Cowboy, scheme: :http, plug: Example.Router, options: [port: 4000]}
    opts = [strategy: :one_for_one, name: Example.Supervisor]"Starting application...")

    Supervisor.start_link(children, opts)

# router.ex
defmodule Example.Router do
  use Plug.Router

  plug :match
  plug :dispatch

  get "/" do
    send_resp(conn, 200, "Welcome")

  get "/start" do
    send_resp(conn, 200, "here is start page!")

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

  get "/api" do
    send_resp(conn, 200, "need to upforward websocket request to")

  match _ do
    send_resp(conn, 404, "Oops!")

The resposity of the project is GitHub - hrzyang/elixir_plug_cowboy_example: study elixir web framework plug_cowboy

hope you can help me, thanks very much !

Check out main_proxy/cowboy2_handler.ex at c75ec91861fec1f8d6a4ec89e4a6809fa6974b02 · Main-Proxy/main_proxy · GitHub.

It supports proxying normal HTTP and Websockets, maybe you can extract the code you need.

1 Like

This one proxies directly to elixir code, not to another http endpoint.

1 Like

thank you very much ! But I really don’t know how to integrate it into my project. Could you send the complete code to my email: or pull and request to my git repo.

You’ll need to show some effort here. People can point you in directions, but this is not a jobboard, where you can just have other people do work for you. Please consider that.


Thank you for your teaching. I will keep it in my mind. But, how to solve the problem now. I am good at java and javascript .To be honst, I am not family with elixir. what is your idea. Is there some package related the problem ?

To be honest if you’re not experienced in elixir why proxy with elixir in the first place? Put a well known proxy in front of your system (e.g. nginx or caddy) and let that proxy either to elixir or the other downstream service you have.

beause the container in my company only support elixir runtime env.