Giter Club home page Giter Club logo

drab_umbrella_example's Introduction

DrabUmb

An example of a drab app running in an umbrella with server: false and the websockets routing correctly

In this example use MIX_ENV=dev
localhost:4000 should route to the Unf.Endpoint, and 127.0.0.1:4000 should route to the DrabExampl.Endpoint

the key to getting this to work is to use the risky dispatch option from phoenix

(https://github.com/jschoch/drab_umbrella_example/blob/master/apps/rtr/config/dev.exs#L25-L34)

Below is a working config for the dev enf

config :rtr, Rtr.Endpoint,
  http: [
    port: 4000,
    dispatch: [
      {:_, [
        {"/socket/websocket", Phoenix.Endpoint.CowboyWebSocket, {Phoenix.Transports.WebSocket,
            {DrabExample.Endpoint, DrabExample.UserSocket, :websocket}}},
        {"/phoenix/live_reload/socket/websocket", Phoenix.Endpoint.CowboyWebSocket,
          {Phoenix.Transports.WebSocket, {DrabExample.Endpoint, Phoenix.LiveReloader.Socket, :websocket}}
        },
        {:_, Plug.Adapters.Cowboy.Handler, {Rtr.Endpoint, []}}
      ]}]
  ],
  debug_errors: true,
  code_reloader: true,
  check_origin: false,
  watchers: []

Then I used a plug to select the other host routes based on the conn.host

https://github.com/jschoch/drab_umbrella_example/blob/master/apps/rtr/lib/umb.ex

defmodule URtr do
    require Logger
    import Plug.Conn

    @doc false
    def init(default), do: default

    @doc false
    def call(conn, _router) do
      Logger.error "host " <> conn.host <> "path " <> conn.request_path
      case conn.host do
        "localhost" ->
          Logger.error "localhost"
          UnfWeb.Endpoint.call(conn,[])
            |> halt
        "127.0.0.1" ->
           Logger.error "127"
           DrabExample.Endpoint.call(conn,[])
             |> halt
        any -> 
          Logger.error "any"
          UnfWeb.Endpoint.call(conn,[])
            |> halt
      end
    end
end

Wondering how to support multiple drab hosts behind a single router

Along the way I found a few examples that help understand how this works

https://elixirforum.com/t/dynamic-phoenix-routes-using-ets-with-sockets-and-channels/1613 https://elixirforum.com/t/plan-old-websockets-in-phoenix-but-without-magic/10074/19 https://elixirforum.com/t/umbrella-apps-on-heroku-channels-not-connecting/8039/2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.