I’ve been recently playing with Phoenix Channels. It’s what a lot of people call the bread and butter of Phoenix, it’s what makes Phoenix really powerful. I haven’t ran into a problem yet where I had to use them or that they were the fix solution I reached for. I’ve only written 2-3 small production elixir applications.
I’m currently playing with phoenix channels and using them instead of ajax to send data to the server when a user answers questions on a survey. It doesn’t seem like the best problem to be using them for, compared to maybe just ajax calls to a controller when a user answers the questions but it’s something I wanted to explore to solve a new feature request on the application I’m working on.
I’ve been going back and forth on when it makes sense to use channels and it seems like they make the most sense when:
- Sending data bidirectionally fairly often ( like chat )
- When you want to send data from the server to the client ( like notifications )
I’ll give another good example that a co-worker and I recently had a discussion about:
We work in the medical industry and it’s pretty common to have an auto logout feature for our applications. So when a user isn’t active, we log them out.
On an older rails application we have, that is done via a javascript timer on the frontend and a session timeout on the backend. So if a user is on the site and stops using it, they are logged out and redirected, also if they decided to leave the application their session will expire after a certain amount of time as well.
My co-worker thought it would be interesting to see if we could implement that auto logout functionality with phoenix channels, and how well that would work.
So basically when a user visits an authenticated part of our site, we would open a phoenix channel and set the timeout to say 15mins. On channel timeout, we would logout the user. We could also expire the session via a plug like we normally do. This seems to greatly simplify the javascript side and lets us define a logout time that is both used for the channel disconnect time as well as the session disconnect time. So we’re not managing both the frontend and backend with possibly different timers.
On one hand I don’t think this is a good solution, because though it works it seems odd to use a web socket just to timeout and log a user out. Especially because the server has a process just sitting around for this use. I do understand that phoenix is very performant, and this would scale probably well beyond our needs, but it just doesn’t seem like the right solution.
On the other hand, I also hate dealing with a ton of javascript and this might be a good trade off until we ever do get to a problem with scale.
Should I be worried with how I am currently playing with using them?
I also think a more general question is, Phoenix makes it so easy to hookup channels and use them that it seems to be used in ways that may not make sense. When in other development stacks it would be a pain to setup and we would often skip over them unless they really make sense.
So the bigger question is, because phoenix makes them easier to setup and use I wonder if we will see them used in places where we normally wouldn’t, and if that makes it more of a blurry line between when it’s a good idea to use them versus not.