Douvi

Douvi

Test Phoneix - broadcast channel into controller API - ConnTest/ChannelTest

Hi,

I have a controller (API) which send a broadcast into a specific channel. I try to do a test to be sure the broadcast is correctly send, See below my code:

Controller

defmodule MyApp.MessageSenderController do
  use MyApp.Web, :controller

  def send_message(conn, %{"message" => message}) do
    PandaPhoenix.Endpoint.broadcast_from(self(), "channel:lobby", "topic:msg", %{message: message})
    send_resp(conn, :no_content, "")
  end
end

Test

defmodule MyApp.MessageSenderControllerTest do
  use MyApp.ConnCase

  test "send broadcast received with render success", %{conn: conn} do
     MyApp.Endpoint.subscribe("channel:lobby")

     conn = conn
       |> put_req_header("accept", "application/json")
       |> post("/api/message", %{"message" => "Hello World"})

     :timer.sleep(2000)
     assert_receive %Phoenix.Socket.Broadcast{event: "topic:msg", payload: %{"message" => "Hello World"}}
     MyApp.Endpoint.unsubscribe("channel:lobby")

     assert response(conn, 204)
  end
end

Error

No message matching %Phoenix.Socket.Broadcast{event: "topic:msg", payload: %{"message" => "Hello World"}} after 100ms.
     Process mailbox:
       {#Reference<0.0.1.548>, {204, [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "ft62s22sptosv9na2o1nqojn8552prjm"}], ""}}
       {:plug_conn, :sent}

But I received the message into the my tchat, do you know what I did wrong ?

Most Liked

dom

dom

Do you have multiple endpoints? In the controller you’re broadcasting from PandaPhoenix.Endpoint, but in the test you’re subscribing to MyApp.Endpoint.

josevalim

josevalim

Creator of Elixir

Do you have a sample application that reproduces the error? Because the above should work (i.e. I could not spot anything wrong with it).

Where Next?

Popular in Discussions Top

Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
sashaafm
I’m trying to evaluate the best combo/stack for a BEAM Web app. Right now I’m exploring Yaws a bit, after having dealt with Phoenix for a...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New
sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
New
Qqwy
I would like to spark a discussion about the static access operator: .. For whom does not know: it is used in Elixir to access fields of...
New
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
fireproofsocks
This is more of a general question, but I’m wondering how other people in the community think about the pattern matching in function sign...
New
kostonstyle
Hi all How can I compare haskell with elixir, included tools, webservices, ect. Thanks
New

Other popular topics Top

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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement