There’s a bit more to it, but that’s the basic idea. Still, developers are reluctant to choose it because of doubts about its real-world performance, user experience and how much of an advantage it provides over traditional SPA’s. When events are fired, or the server updates, the changes are sent back and forth over the websocket with lightweight JSON messages (all managed by Phoenix) like this: [ Phoenix LiveView is a relatively new but mature platform. # Returns the socket with a new `current_id`Īssign(socket, :current_id, Gallery.prev_image_id(_id)) This example runs “prev” to tell the server that it should show the previous image: def handle_event("prev", _event, socket) do Then you can handle those events with handle_event and the name of the action. The templates indicate which actions should be run for events, in this case phx-click runs the "prev" (previous image) action: Prev The HTML gets rendered server-side (good for SEO), but then the JS establishes a websocket connection and further HTML changes are sent over the websocket. The render function contains an initial template to send. 4 years Elixir experience 3 years Ruby on Rails experience 7+ years overall dev experience. # returning a tuple with the message `:ok` and the new `socket` |> assign(:current_id, Gallery.first_id()) # Adding key-value pairs to the websocket connection
0 Comments
Leave a Reply. |