Using Hotwire with Ruby on Rails
I’ve been pretty quiet recently - that’s because I’ve been having way too much fun playing with Hotwire.
Hotwire’s a Javascript library that adds really easy “single-page-application” and web-socket support to your web application - without having to learn a whole new front-end framework.
It intercepts links and form submissions, so instead of doing a full page refresh, it looks at the response coming back from the server, chooses which parts of the page to replace and just swaps them out inline. Not only does this mean the browser doesn’t need to redraw the whole page, it also means all your Javascript and other assets don’t need to be reloaded - making things really fast.
In addition, you can specify areas of the page to be loaded on demand - so your initial page load can be really fast and the other stuff deferred till when it’s needed (for example, I’ve done a menu that only loads when you click the drop-down icon - and then it can check your permissions in real time to make sure you only have access to the correct options).
Finally it includes a layer over web-sockets so as stuff changes on the server, you can broadcast those changes to your client - as HTML fragments. It uses the same swapping technique as before to update your page without a full refresh - so you get live updates in two lines of code.
The advantage of not using a front-end framework for this kind of stuff (apart from being able to stick with the technology you’re already familiar with) is that you don’t need to replicate your logic. You’ve already written your models and business rules on the server - so why duplicate those rules and structures on the client as well?
If you want to see it in action, I’ve done a quick video and, I’m so impressed, I’m going to be doing more in-depth stuff on it as the next few weeks progress.