Phoenix liveview1/4/2024 SolutionĪ LiveView can't write to the session directly, so we'll instead have it trigger an Ajax request to our Phoenix app, which updates the session. Unlike localStorage, it can be read server-side and can thus be present on the very first render of a LiveView. Like localStorage, the data is stored client-side, so there's no need for a database dependency, and no need to worry about when to expire old session data. What is Phoenix LiveView LiveView is one of the most exciting features in the Phoenix web framework. ![]() LiveView is stateful, but keeps its state in a process that quits when you leave the page.Īnd since it only makes a single HTTP request initially to load the page (after that, it's all via WebSocket), we can't just set cookies or modify the Plug/Phoenix session as directly as we're used to – but as it turns out, we can do it in a roundabout way.īy default, Phoenix session data is stored in a cookie that is signed but not encrypted – the user can read it but can't modify it. This time, were going to improve real-time communication between the apps users using Phoenix PubSub, and use the Pow library to add secure user authentication. The problem, again, is that there's no obvious way to save persistent session data in LiveView.įor example, I might implement a chat and want it to remember your username even if the page is reloaded, or you quit your browser and come back the next day. Weve already bootstrapped our Phoenix LiveView-based Messenger apps database structure and a first LiveView page. I’ve written previously about LiveView and why I think it’s such an amazing tool. I decided to build a simple Decision Matrix app named Ruminate.io.For a deeper dive into the app, check out this series of articles that recounts the entire product development journey. That means you can build highly interactive single page-style webapps without writing a line of JavaScript. The release of Phoenix 1.5 with LiveView integration gave me the perfect excuse to finally build a LiveView App. Phoenix’s LiveView is great, and has drastically reduced the need for client-side Javascript code for basic use cases in Phoenix apps. LiveView powered applications are stateful on the server with bidirectional communication via WebSockets, offering a vastly simplified programming model compared to JavaScript alternatives. Third time's the charm – I now have a solution for storing it in the actual Plug/Phoenix session, based in large part on a solution that Martin Svalin shared. The Phoenix web framework takes advantage of these features and enables LiveView for highly interactive server-rendered content. Phoenix LiveView is an exciting new library which enables rich, real-time user experiences with server-rendered HTML. I wrote previously about how to save persistent session data in the DB or in localStorage with Phoenix LiveView.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |