Connecting Golang microservice to Phoenix channel through Gorilla Websockets

Is it possible to connect to a phoenix channel from a separate microservice written in go? I’ve looked at go websockets, but not sure how to implement as there aren’t any examples related to phoenix channels

It is possible, but you would need to write a wrapper around the go websockets (I’m not sure if this has been done) because Phoenix uses it’s own protocol ontop of websockets.

Would this work?

1 Like

I can’t say for certain, I have never used the project. But it looks like it may be missing some features of the protocol. I can’t seem to find any mention of phoenix’s presence, but that may not be an issue depending on how you want to use it.

I’ve used that package previously and it worked. However I scrapped and wrote my own (sorry closed source) due to the need for a re-connection policy and a fallback to the long poll transport. I’m still using the gorilla websocket package for the raw connection parts though.

So yes, completely possible and not all that hard :slight_smile:

The best place to look at how to write the client code is the phoenix.js socket code. It contains all the nitty gritty details you need to implement it correctly.

Pro Tip: Don’t forget to send heartbeats with an idle connection :slight_smile:

Cheers,
Ben

1 Like

@nerdyworm thanks for the tip, is it possible to use redis pubsub with both phoenix and golang instead? If so, how do we use phoenix with redis pubsub?

I’m sure it’s possible :slight_smile:, but I haven’t gone down that rabbit hole yet so I can’t be 100% certain that you wouldn’t run into a roadblock.

So I made some headway thanks to somebody from stackoverflow. I’m just wondering if you had experience debugging the Join function from the golang side of things.

Currently I’m receiving an error from phoenix:

Ranch protocol #PID<0.437.0> of listener MyAppWeb.Endpoint.HTTP (cowboy_protocol) terminated
phoenix_1  | ** (exit) exited in: Phoenix.Endpoint.CowboyWebSocket.resume()
phoenix_1  |     ** (EXIT) an exception was raised:
phoenix_1  |         ** (FunctionClauseError) no function clause matching in 
Phoenix.Socket.Message.from_map!/1

However I’m unable to decipher what is causing this issue. I have a Join function that I call from the simonsargent gophoenix library. I have no problems connecting to the socket client on golang, but joining a channel raises the error above and my golang microservice disconnects from the socket immediately

1 Like