hrzyang

hrzyang

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 (http://127.0.0.1/api) to another server (http://127.0.0.1:8080/abc). 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]

    Logger.info("Starting application...")

    Supervisor.start_link(children, opts)
  end
end

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

  plug :match
  plug :dispatch

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

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

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

  get "/api" do
    send_resp(conn, 200, "need to upforward websocket request to http://127.0.0.1:8080")
  end

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

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 !

Most Liked

LostKobrakai

LostKobrakai

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.

c4710n

c4710n

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.

LostKobrakai

LostKobrakai

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

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement