J0 / phoenix_gen_socket_client

Socket client behaviour for phoenix channels
MIT License
232 stars 48 forks source link

Listening to Phoenix Channel from Erlang Node #39

Closed nitinjain1105 closed 5 years ago

nitinjain1105 commented 5 years ago

Hi @aircloak

I am trying to listen to phoenix channel from Erlang Node, I see that this library works when we are trying to listen from an Elixir Node.

Do you offer similar support for listening to Phoenix Channel from Erlang Node as well?

Thanks Nitin

sasa1977 commented 5 years ago

If I understand this right, you have a pure Erlang project which needs to act as a Phoenix client, right?

We didn't implement an Erlang equivalent ourselves. One thing you could try is integrating Elixir into your Erlang project, which would make it possible to use this library. If your Erlang project is built with rebar3, you could look into this plugin. I haven't tried it myself though, so I don't know if it will work, and how exactly to use it.

Other than that, the only other suggestion I have is to implement the client yourself. You basically need to pick a plain websocket client (this is the one we use, and it's written in pure Erlang) and then implement the handling of Phoenix messages. Phoenix protocol is not very complicated, but unfortunately it's not documented. Our protocol handling code can be found in this function.

nitinjain1105 commented 5 years ago

Hi @sasa1977

Using the plugin and downloading Elixir library (phoenix_gen_socket_client) into Erlang node worked. But I am not able to start the socket process from Erlang Node. Any help would be highly appreciated.

This is what I tried doing in start_link of erlang gen_server -

start_link() -> URL = "ws://localhost:XXXXX/task/websocket",

'Elixir.Phoenix.Channels.GenSocketClient':start_link( ?MODULE, 'Elixir.Phoenix.Channels.GenSocketClient.Transport.WebSocketClient', URL, [{user_id, atom_to_list(node())}], {name, 'Chimera.Agent.WebSocketClient'} ).

============ Error Message

=SUPERVISOR REPORT==== 30-Jan-2019::15:17:07.342000 === supervisor: {global,chimera_sup} errorContext: start_error reason: {'EXIT', {undef, [{'Elixir.GenServer',start_link, ['Elixir.Phoenix.Channels.GenSocketClient', {task_channel_controller, 'Elixir.Phoenix.Channels.GenSocketClient.Transport.WebSocketClient', "ws://localhost:14000/task/websocket", [{user_id,"nonode@nohost"}]}, {name,'Chimera.Agent.WebSocketClient'}], []}, {supervisor,do_start_child_i,3, [{file,"supervisor.erl"},{line,379}]}, {supervisor,do_start_child,2, [{file,"supervisor.erl"},{line,365}]}, {supervisor,'-start_children/2-fun-0-',3, [{file,"supervisor.erl"},{line,349}]}, {supervisor,children_map,4, [{file,"supervisor.erl"},{line,1157}]}, {supervisor,init_children,2, [{file,"supervisor.erl"},{line,315}]}, {gen_server,init_it,2, [{file,"gen_server.erl"},{line,374}]}, {gen_server,init_it,6, [{file,"gen_server.erl"},{line,342}]}]}} offender: [{pid,undefined}, {id,task_channel_controller_id}, {mfargs,{task_channel_controller,start_link,[]}}, {restart_type,permanent}, {shutdown,infinity}, {child_type,worker}]

sasa1977 commented 5 years ago

Theundef, [{'Elixir.GenServer',start_link part at the beginning of an error seems to indicate that Elixir standard library is not included in the system. Unfortunately, as I said, I'm not familiar with the plugin, so I can't help you more at the moment. Time-permitting, I'll try to play with this on my own, and if I get somewhere, include a demo project, but I can't really say when/if this will happen.

nitinjain1105 commented 5 years ago

Hi @sasa1977

I tried following this link: https://www.rebar3.org/docs/using-available-plugins#section-elixir-dependencies 1- I added the plugins, hooks, overlay and modified vm.args then I followed - https://www.rebar3.org/docs/releases#section-deployable 2- rebar3 release -d false 3- rebar3 as prod tar 4- mkdir myrel 5- mv myrel-0.1.0.tar.gz myrel/ 6- cd myrel 7- tar -zxvf myrel-0.1.0.tar.gz 8- bin/myrel console

I still get undefined error. Any help would be much much appreciated. Hope to hear back from you.

sasa1977 commented 5 years ago

I just quickly tried running an Elixir project with rebar3, and hit a few minor bumps, so here are some tips:

That got me to the point where Elixir is working in a local shell (I didn't try a release though). Unfortunately, I wasn't able to add phoenix_gen_socket_client as a dependency, because rebar3 complains:

===> Dep poison has invalid version ~> 2.0 or ~> 3.0

Which is caused by this line in phoenix_gen_socket_client.

This dependency requirement is valid, but it seems that rebar3 doesn't support it. So it looks like at this point the thing won't fly, but I didn't spend any more time looking into this, so I might be wrong. I suggest you turn to rebar3/rebar_mix related channels for further help.

nitinjain1105 commented 5 years ago

Hi @sasa1977

So I got the resolution for undef error I had earlier. I was not adding elixir/bin path to my erl -pa option.

I have just one last piece remaining: I used to write my init function in Elixir like this -

  def init(url) do
    Agent.start_link(fn -> %{} end, name: Dispatcher.Agent)
    {:connect, url, [{"user_id", Atom.to_string(node())}], %{}}
  end

Cannot figure out how it would be for erlang. There are no agents in Erlang, so how should I go about that!!

sasa1977 commented 5 years ago

If you're able to link everything properly, then you can still invoke Agent from your Erlang code by using 'Elixir.Agent':start_link(...). For more information about Erlang vs Elixir see this page, in particular this section.

Also, for future reference, please don't use this project issue board for general purpose questions about Elixir or Erlang. Such questions do not belong here, and I advise asking them on dedicated community exchange sites, such as Elixir forum.