Using phoenix channels outside an elixir phoenix webapp

Hi there,

I have a PHP app (it’s big can’t migrate it to elixir) so for pseudo real time communications, I’m using a request that uses Server Sent Events that start a redis subscribe to specific channels and on each message I sent to the client, so I can send notifications/progress/etc to the client from the server. The issue is that this requests are long lived, and they are using too much resources in the server.

I’m trying to move to websockets instead of SSE, but in php it looks super complex. I was wondering if I can use channels outside elixir, looks like the way to send messages is simple enough I can write a client in php, but tried to add the js socket client to my app and it didn’t work. It’s trying to open the websocket connection to the same host the website is running instead of the webserver where phoenix app is running, not sure how to specify the right host in the js client.

And what is the best way to secure this scenario?

Thanks in advance

Hey @marceloandrader let me see if I understand the scenario you are trying to make work.

  1. You have an existing PHP app that serves HTTP requests, including SSE.
  2. You have a Phoenix app that runs on a different domain (host)
  3. You want your pages from the PHP app in (1) to include Javascript that talks to (2)?

Yes exactly. I can make it run the phoenix app behind nginx on the same domain. Haven’t tested that yet.

If what you want to do is, connect to the channels from the JS side. You only need to include the phoenix javascript and setup the connections as it is done in a normal phoenix app.


Thank you all, reporting that it worked flawlessly. For the PHP side talking to elixir, I just decided to add an endpoint to receive the message and broadcast it.

Only thing left is authentication, in the examples I see that from the js tries to send a userToken, but in my case I’ll need to create this token in PHP to then send in my js file.

Is there any other implementations besides elixir of this signing method: