For learning purposes I created this simple TODO app that uses Phoenix Channels instead of Controllers to CRUD lists
and todos
:
https://chandothis.herokuapp.com/
and the code is here:
https://github.com/iacobson/chan_do_this
Now I want to continue the experiment and send channels messages from a different Phoenix (or even just Elixir) app to the existing Heroku app, let's say to create a new list. But here I got into trouble.
What would be the way to connect to the Heroku socket ?(wss://chandothis.herokuapp.com/socket/websocket?token=undefined&vsn=1.0.0) and join the channel "lists"
Update 1
Tried using elixir_socket (https://github.com/meh/elixir-socket)
{:ok, socket} = Socket.connect("wss://chandothis.herokuapp.com/socket/websocket")
{:ok,
%Socket.Web{extensions: nil, headers: %{}, key: "Zm9yayB0aGUgZG9uZ2xlcw==",
mask: true, origin: nil, path: "/socket/websocket", protocols: nil,
socket: {:sslsocket, {:gen_tcp, #Port<0.7049>, :tls_connection, :undefined},
#PID<0.372.0>}, version: 13}}
{_, _, pid} = socket.socket
send pid, {:send, %{topic: "phoenix", event: "phx_join", payload: %{}}}
but getting this:
[info] [83, 83, 76, 58, 32, 71, 111, 116, 32, 117, 110, 101, 120, 112, 101, 99, 116, 101, 100, 32, 105, 110, 102, 111, 58, 32, [123, [[123, ['send', 44, [35, 123, [['event', ' => ', [60, 60, '"phx_join"', 62, 62]], 44, 10, [' ', [' ', 32, 32, 32], 32, [32, [' ', 32, 32, ...], ' ', 32, 32, ...], 32, [' ', 32, 32, ...], ' ', 32, 32, ...], ['payload', ' => ', '\#{}'], 44, 10, [' ', [' ', 32, 32, ...], 32, [32, ...], 32, ...], ['topic', ' => ', [60, 60, '"phoenix"', ...]]], 125]], 125], 44, 10, [32, [32, [' ', 32, 32, 32], ' ', 32, 32, 32], 32, [' ', 32, 32, 32], ' ', 32, 32, 32], 'tcp_error', 44, '#Port<0.7049>'], 125], 32, '\n']
Update 2
Another attempt with Erlang websocket_client (https://github.com/sanmiguel/websocket_client)
and used the below as example:
https://github.com/phoenixframework/phoenix/blob/master/test/support/websocket_client.exs
{:ok, socket} = Phx.WebsocketClient.start_link(self(), "wss://chandothis.herokuapp.com/socket/websocket?token=undefined&vsn=1.0.0")
Phx.WebsocketClient.join(socket, "lists", %{})
getting the following error:
[error] State machine #PID<0.391.0> terminating
Last message in was {:send, %{event: "phx_join", payload: %{}, ref: "1", topic: "lists"}}
When State == :disconnected
Data == {:context, {:websocket_req, :wss, 'chandothis.herokuapp.com', 443, '/socket/webs`enter code here`ocket?token=undefined&vsn=1.0.0', :infinity, :undefined, 1, :undefined, {:transport, :ssl, :ssl, :ssl_closed, :ssl_error, [mode: :binary, active: true, verify: :verify_none, packet: 0]}, "mR8BigxTXTlwBgxhiE63gA==", :undefined, :undefined, :undefined, :undefined, :undefined}, {:transport, :ssl, :ssl, :ssl_closed, :ssl_error, [mode: :binary, active: true, verify: :verify_none, packet: 0]}, [], {:wss, 'chandothis.herokuapp.com', 443, '/socket/websocket?token=undefined&vsn=1.0.0'}, {Phx.WebsocketClient, %{ref: 1, sender: #PID<0.384.0>}}, "", false, 0}
Reason for termination = {:error, :disconnected}