Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I connect to a remote / external Phoenix app channel from another Phoenix app?

I am looking to incrementally port over a legacy API application to Phoenix. Ideally, I would carve out the subdomains of my application into separate Phoenix apps and host them on different server nodes.

I am hoping to use Phoenix channels to facilitate app to app communication. How do I connect one Phoenix app to another's channels and topics? For example, I would like to set up a 'jobs.foo.com' subdomain with WebSockets like:

socket "/ws", Jobs do
   channel "jobs:work_orders", WorkOrdersChannel
end

From another Phoenix app (ex: Reports) I would like to pub/sub to that Jobs channel topic. Would it be advisable to create a Phoenix.Endpoint Jobs lib, in Reports, and configure that Jobs.Endpoint url in config.exs? Could I then make use of something like Phoenix.PubSub.subscribe/4 with that named pubsub endpoint?

I am not sure if what I am thinking makes sense. If there is a more advisable approach, I am open to suggestion.

-- EDIT --

Just to clarify what I am trying to accomplish ... I am wanting to make one Phoenix app connect to another via a WebSocket client. That will allow me to have apps, with different domain logic, communicate via events over channels and topics of common interest.

like image 419
Mike Clymer Avatar asked Jul 06 '15 22:07

Mike Clymer


2 Answers

Ok, I figured out a way to create WebSocket clients in my various Phoenix apps and allow app to app communication over remote channels and topics. I took my cues from:

channel_test.exs

and

websocket_client.exs

I basically copied the websocket_client.exs code into my app's ../lib directory, then loaded and alias'ed it. After that I could connect one app to the remote socket of another app, much like it is done in channel_test.exs. I spun up two separate Phoenix apps, on two different machines, and was able to send, and receive messages across the channels.

Don't forget to add {:websocket_client, github: "jeremyong/websocket_client"} to your mix.exs dependencies.

like image 140
Mike Clymer Avatar answered Oct 15 '22 02:10

Mike Clymer


I think you might want to look into Phoenix.PubSub - as long as you manage to connect your nodes into a cluster (or even without that, using a DB or Redis backend) you should be able to make them talk to each other over that.

like image 27
Paweł Obrok Avatar answered Oct 15 '22 03:10

Paweł Obrok