Websockets and communicating to systems built with other languages

Hey guys,

Is-it possible to communicate between a phoenix/Elixir server with other servers built in Node.js, Python, Ruby, PHP… using websocket protocol ?

I’ve tried to do it but no success… I have the problem of Phoenix channels protocols…

2 Likes

Why do you want to do is? Are the “normal” webmessages (json or xml over http) not enough?

1 Like

Yes, channels can be used for server <-> server communication, but you need to write the channels client in your target language of choice.

3 Likes

You can also communicate just via a custom TCP protocol too if you want, you can do anything related to networking and communication pretty easily here. :slight_smile:

3 Likes

Yea, then you just need to write your own wrapper fo all TCP :smiley: Done that, however that was time consuming.

1 Like

There are libraries in Hex.pm that make that very simple (I had my own customized version of gen_tcp back in the erlang days I used for that). :slight_smile:

3 Likes

Ok, nice to know that, because once, when I actively used node.js as my backend service language, that was interesting time for me :smiley:

1 Like

Ogod I feel sorry for you… o.O

Handling raw packets in node is an utter pain! The bit/binary syntax makes parsing raw packets on the EVM a joy! The matchers actually read like the spec. ^.^

2 Likes

Another option would be to use websockets without Phoenix channels. I believe this is possible to setup by installing a custom Cowboy handler (is that right @chrismccord?). The benefit is that you can then use plain websockets, without needing to implement the channels clients in different languages. This should simplify/reduce your work in those other technologies, because you could just use available websocket client libraries to communicate with your Elixir system.

However, I also think that @grych has a point with his question. You may want to consider if you really need websockets. In many cases, standard HTTP requests might be a simpler option.

6 Likes

If you have control over the remote servers, I would definitely think about a REST API or something like RabbitMQ for message brokering. If websockets are your only available interface, then I look at using something like Gun https://github.com/ninenines/gun for your client. You’ll need a websocket client in order to connect to the remote servers.

1 Like

I concur. We use RabbitMQ to talk between Ruby and Elixir and it works really good, with minimal boilerplate code. Definitely simpler than building API.

1 Like

We use BERT (which is maybe outdated), but it works very well to talk between Ruby and Elixir.
It’s fast, simple to implement and there are packages for Ruby, Python, Java and other languages.

It’s a pity that it’s quite neglected now. :slight_frown:

2 Likes

No one seems to have mentioned Erlang Ports or the Interfaces.

I’ve used Erlang Ports to interface with Ruby in the past and it was quite easy and successful. Erlang Ports and Port Drivers. There are also a couple of libs which making using Ports easier, like erlport.

For C you may use Erl_interface and for Java there’s JInterface. I’ve used the Java one before and I thought it was pretty good. There’s a complete Erlang <-> Java mapping of types, and Java nodes will be seen by the BEAM like if they were Erlang nodes.

1 Like

We have a N individual web applications which do not need to be on the same host and want to do communication between them. I do not think that ports are suitable for this-

1 Like

My thinking too, unless I don’t understand something about Ports.

1 Like

Yeah 90% of the time I do a Port using the normal Erlang Term Format (there are libraries for it in about every language, and it is almost trivial to write your own as I’ve done a few times), 5% of the time I make another language into a Node, and the other 5% I’ve made a NIF.

But really, just using a Port with the normal erlang term format is so wonderful and easy to use while being nicely compact.

If however they are web applications and already have, say, JSON communication points, just use the existing ones, if you need to make something new I’d use either JSON or the erlang term format (if a library exists there for it, which it does for Ruby as I recall).

1 Like

We finally choose RabbitMQ :wink: thx

1 Like