Talking to Phoenix channels from the command line

I’d like to have some develoment scripts that talk to a running local server, acting as clients using WebSocket:

mix run utils/simulate_clients.exs

Is it possible to write such script using the Phoenix API? Or do I need to use something like phoenix_client?

(I have been able to prototype something using the Phoenix modules for testing, but those don’t communicate through the server.)

1 Like

I’ve used the linked Phoenix client library to write an elixir consumer and it worked great.

It feels like possibly the wrong angle here. Is there a way to extract the channel functionality into a shared Module that you can call directly? If you use remote_console to access the running application, you’d be in the same application and would have the same result from running the same code. It’s hard to say what’s best without knowing why you are communicating with the channel this way.

Also, you could technically start your own channel process and communicate with it directly, but I’ve never tried that so I’m not sure what would go into it. Probably not the right call here.

Regarding the use case, this application has no web interface, its clients run in mobile phones. Having a script like that allows me to simulate usage and exercise the system for real in development.

Gotcha. I would recommend Phoenix client in that case. I found it surpringly pleasant to use.

1 Like

It’s websockets, can you use https://github.com/websockets/wscat and then use cli to execute commands?

That’s a great trick, and it is used by Real-Time Phoenix to let you understand what’s really going on.

But for my use case I prefer a higher abstraction.

2 Likes