mtrudel/bandit

(FunctionClauseError) no function clause matching in Bandit.HTTP1.Handler.handle_info/2

TamLyNhat opened this issue · 2 comments

Hi,

I created a basic game channel, and I use PubSub.subscribe in the LiveView, after loading the page, user joins channel and broadcast the message, but it crashes in PubSub.broadcast message:

�[0m�[22m[info] JOINED game:1 in 155µs
  Parameters: %{"params" => %{"user_id" => "abc"}} file=lib/phoenix/logger.ex 
�[0m�[31m[error] GenServer #PID<0.701.0> terminating
** (FunctionClauseError) no function clause matching in Bandit.HTTP1.Handler.handle_info/2
    (bandit 0.7.7) /Users/tamly/dev/sport_app/deps/thousand_island/lib/thousand_island/handler.ex:5: Bandit.HTTP1.Handler.handle_info(%{test: "data"}, {%ThousandIsland.Socket{socket: #Port<0.12>, transport_module: ThousandIsland.Transports.TCP, read_timeout: 60000, span: %ThousandIsland.Telemetry{span_name: :connection, telemetry_span_context: #Reference<0.3032579094.363069444.191205>, start_time: -576460741956052833, start_metadata: %{remote_port: 57336, remote_address: {127, 0, 0, 1}, telemetry_span_context: #Reference<0.3032579094.363069444.191205>, parent_telemetry_span_context: #Reference<0.3032579094.363069444.191121>}}}, %{opts: %{websocket: [], http_1: [], http_2: []}, plug: {Phoenix.Endpoint.SyncCodeReloadPlug, {StreamingServiceWeb.Endpoint, []}}, handler_module: Bandit.HTTP1.Handler, requests_processed: 5}})
    (stdlib 5.0.2) gen_server.erl:1077: :gen_server.try_handle_info/3
    (stdlib 5.0.2) gen_server.erl:1165: :gen_server.handle_msg/6
    (stdlib 5.0.2) proc_lib.erl:241: :proc_lib.init_p_do_apply/3
Last message: %{test: "data"}
State: {%ThousandIsland.Socket{socket: #Port<0.12>, transport_module: ThousandIsland.Transports.TCP, read_timeout: 60000, span: %ThousandIsland.Telemetry{span_name: :connection, telemetry_span_context: #Reference<0.3032579094.363069444.191205>, start_time: -576460741956052833, start_metadata: %{remote_port: 57336, remote_address: {127, 0, 0, 1}, telemetry_span_context: #Reference<0.3032579094.363069444.191205>, parent_telemetry_span_context: #Reference<0.3032579094.363069444.191121>}}}, %{opts: %{websocket: [], http_1: [], http_2: []}, plug: {Phoenix.Endpoint.SyncCodeReloadPlug, {StreamingServiceWeb.Endpoint, []}}, handler_module: Bandit.HTTP1.Handler, requests_processed: 5}} file=gen_server.erl 
�[0m�[31m[error] Process #PID<0.701.0> terminating

game_channel.ex:

def join("game:" <> game_id, %{"params" => %{"user_id" => _user_id}} = payload,
           socket) do
    topic = "game:" <> game_id
    :ok = PubSub.broadcast(@pubsub_chat_room, topic, %{test: "data"})
    {:ok, socket}

chat_room_live_view.ex

def mount(%{"game_id" => game_id, "user_id" => user_id}, _, socket) do
    topic = "game:" <> game_id
    # subscribe to chat room with game id.
    :ok = PubSub.subscribe(@pubsub_chat_room, topic)
    map = [%{user_name: "john", messages: "hehe"}]
    users_online = ["john"]
    {:ok, socket}
  end

game_socket.js

import {Socket} from "phoenix"
let socket = new Socket("/socket", {params: {token: window.userToken}})

socket.connect()

// get game_id (1) from path chat_room/1/abc
const pathname = window.location.pathname;
var array = pathname.split ("/"); // split the string by "/"
var game_id = array [2]; // get the third element of the array
var user_id = array [3];
// Now that you are connected, you can join channels with a topic:
let channel = socket.channel("game:" + game_id, {params: {user_id: user_id}})

console.log(channel)

channel.join()
  .receive("ok", resp => { console.log("Joined successfully", resp) })
  .receive("error", resp => { console.log("Unable to join", resp) })
export default socket

route.ex:

  scope "/", StreamingServiceWeb do
    pipe_through :browser

    live "/chat_room/:game_id/:user_id", ChatRoomLiveView

  end

Do you have any idea how to fix this error ?

Thanks for your support.

Tam

I believe this is a dupe of #141 & #84; the former has the better description of what's going on, but the issue is that your mount code needs to discriminate on the value of connected? since LiveView calls this function at two places in the connection lifecycle.

Closing for hygiene. If the above pointers do NOT in fact answer the issue, please feel free to reopen!

Thanks for the issue!