1

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}

iacobSon
  • 133
  • 10
  • What have you tried doing thus far? Phoenix channels are simply web sockets. – Justin Wood Nov 12 '16 at 15:09
  • so what I'm thinking is if I can connect with Phoenix.Socket.Transport.connect/6, then I would be able to use the default callbacks like `handle_in` Eg. have a `def handle_in("created" .....)` in the local app, and then the Heroku app can do a push to this "created" after creating a list. The blocker is that I cannot make them share the same socket – iacobSon Nov 12 '16 at 16:18
  • Can it be a problem with `:check_origin` config in your config/*.exs? – Евгений Масленков Nov 14 '16 at 07:07

0 Answers0