shahryarjb

shahryarjb

How to connect Phoenix Channels in Postman

Hi friends,
It is my first experience to work with Phoenix Channels, so I have read some documents, but I want to test my socket with postman.

socket "/socket", ChatFCoinWeb.ChatBotSocket, websocket: true, longpoll: false

I added this in my endpoint, but in post man I can not connect to it with this address: ws://localhost:4000/socket/ I should use ws://localhost:4000/socket/websocket

I have a channel like this:

channel "room:*", ChatFCoinWeb.RoomChannel

So I want to send data or connect to my room

defmodule ChatFCoinWeb.RoomChannel do
  use Phoenix.Channel
  def join("room:lobby", _message, socket) do
    IO.inspect("hey")
    {:ok, socket}
  end

  def join("room:" <> _private_room_id, _params, _socket) do
    {:error, %{reason: "unauthorized"}}
  end

  def handle_in("new_msg", %{"body" => body}, socket) do
    broadcast!(socket, "new_msg", %{body: body})
    {:noreply, socket}
  end
end

As you see this image I got server error, in my console:

[error] Ranch listener ChatFCoinWeb.Endpoint.HTTP had connection process started with :cowboy_clear:start_link/4 at #PID<0.871.0> exit with reason: {%Phoenix.Socket.InvalidMessageError{message: "missing key \"event\""}, [{Phoenix.Socket.Message, :from_map!, 1, [file: 'lib/phoenix/socket/message.ex', line: 35]}, {Phoenix.Socket, :__in__, 2, [file: 'lib/phoenix/socket.ex', line: 471]}, {Phoenix.Endpoint.Cowboy2Handler, :websocket_handle, 2, [file: 'lib/phoenix/endpoint/cowboy2_handler.ex', line: 145]}, {:cowboy_websocket, :handler_call, 6, [file: '/Users/shahryar/Documents/Programming/Docker/ChatFCoin/deps/cowboy/src/cowboy_websocket.erl', line: 528]}, {:cowboy_http, :loop, 1, [file: '/Users/shahryar/Documents/Programming/Docker/ChatFCoin/deps/cowboy/src/cowboy_http.erl', line: 257]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 226]}]}

Update:

I sent like this:

{
    "topic": "room:lobby",
    "event": "new_msg",
    "payload": {
        "body": "hi I am in it"
    },
    "ref": "98fcec60-9538-4b6e-8d68-f8a351ddf650",
    "join_ref": "4c11a27f-fe74-4294-86f1-e07dc29f4e28"
}

and I got this:

{
    "event": "phx_reply",
    "payload": {
        "response": {
            "reason": "unmatched topic"
        },
        "status": "error"
    },
    "ref": "98fcec60-9538-4b6e-8d68-f8a351ddf650",
    "topic": "room:lobby"
}

Please help me to connect with each my room and channel
Thank you

Most Liked

shahryarjb

shahryarjb

I think I should use phx_join and after that I can use new_msg ? as handle_in? Am I right?

  • :ref - The unique string ref
  • :join_ref - The unique string ref when joining

ref key should be changed in every request?

tomkonidas

tomkonidas

I use wscat for this on the cli.

Where Next?

Popular in Questions Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
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
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
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
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement