How does elixir genserver work?
Are you wondering how to deal with long-term loading of Elixir or Phoenix application data? That’s where GenServer comes in handy in Phoenix Live View. I’ll show you how to use it! According to the documentation, Phoenix Live View aims to be a rich real-time user experience with server-side rendered HTML. Therefore, in my opinion, it should be as light as possible. It can’t slow down as it affects the user experience (UX). But sometimes there are circumstances when it is really inevitable to load thicker data. I ran into this problem while creating my previous app. Today I will show you how to make a simple loading screen while assigning data to a socket. Whether it’s a really long calculation or a lengthy database query, the solution that came to mind is really simple. It takes advantage of the fact that the live preview underneath is nothing but a process that receives messages and updates its status. Therefore, we will use the GenServer tools to make this work.
What is GenServer in Elixir?
GenServer is an Elixir abstraction surrounding the process and its tools. Maintains the state and has a toolkit for modifying it already implemented. This includes tracking and reporting errors. Once you understand the basics of working with GenServers, it becomes really convenient to use. Live View also uses GenServer and therefore we can use their functionality.