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

2 Likes

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?

1 Like

@shahryarjb Iā€™m trying to do the same thing ā€“ set up a Postman collection mimicking what phoenix.js does.
Were you able to figure this out?

Sorry, I forgot what I did on that time, but now I use javascript browser api for websocket, if I want to try

I use wscat for this on the cli.

1 Like