DEV Community

David Marko
David Marko

Posted on

Not a Go LiveView developer yet? Try to guess what this code is doing, though.

My common practice for evaluating new technology, framework, or programming approach is based on the first feeling I have from the code presented. What is the cognitive effort to understand “the idea”? Things have to “look nice and be polished” in some way to convince me. I can still remember that overall ‘wow effect’ when seeing my first Phoenix LiveView presentation a few years ago. Such a small amount of code and a great level of expressiveness so even not being a daily Elixir developer make me an idea, of how this is supposed to work and what the code is doing.

LiveView implementation for Go raised the same type of feelings in me when I went through this for the first time.

Just make a quick review of the code below without seeing any other details:
Example

That is like we write stuff in Go LiveView implementation.

ad 1) In HTML we use tag attributes to specify LiveView-related actions. LiveView makes those things actually ‘live and working’, no other JS code is needed. We have defined LiveView click event here. In other words, we just said: “send ‘temp-up’ event back to backed, when this button is clicked in the browser”. LiveView detects this attribute in HTML and provides the appropriate actions that will make a backend call through the websocket. LiveView establishes a websocket connection to our backend when a web page is loaded in a browser for the first time.

ad 2) We reference variables from our backend model using Go Template syntax. LiveView keeps those values updated whenever the backend representation is changed. LiveView uses websocket push technology under the scene here.

ad 3) We tight ‘temp-up’ event front end is emitting to our backend handler. LiveView calls our handler whenever a ‘temp-up’ event is emitted. We can just rely on that.

To learn more, you can take a look at my previous articles on LiveView here:

Are you still with me? Nice. If you are interested in details and want to get a whole idea of how you can use the LiveView programming approach in Go for your new or existing projects, you are welcome to take part in this new Udemy Course “Mastering Live(View) development in Go”.

Udemy course

Top comments (0)