10
votes

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.

2

2 Answers

6
votes

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.

5
votes

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.