Explore
This are public items saved by our community
How to Connect, Write and Test Your Phoenix App In Livebook
Sometimes, I just want a quick way to validate my logic through quick tests without having to open my code editor.
For example, when I want to confirm that:
- A function transforms a map as I expect it to.
- Import CSV data in the database using existing Ash resource.
- And more.
Today, I am showing you how to connect your existing Ash and Phoenix app with Livebook and be able:
- To call any module and function from your app in Livebook .
- Write new module in Livebook , like liveview and call them in your phoenix app.
- Or even have fun writing your code and seeing the results just there.
First, I am assuming that you already know what Livebook is, and have it installed on your local machine. If not, then head to https://livebook.dev/ and install it.
Second, I am assuming that you have a phoenix application in your computer. If not go to https://www.phoenixframework.org/ and follow instruction to have a phoenix application in your computer.
If you are here, I am assuming that you have the livebook and a working Phoenix apps on your computer.
Erlang distribution system is what makes it possible to connect your app to livebook and vis versa. And we only need to add 2 information to your normal iex -S mix phx.server and make it connectable from Livebook.
You need to:
- Start your application with iex --name myapp@127.0.0.1 --cookie secret -S mix phx.server
- Open your livebook and specify myapp@127.0.0.1as node andsecretas password or cookie to your app.
That’s all. Let us see it in action.
Start your Phoenix app:
iex --name socialfund @127 .0.0.1 --cookie secret -S mix phx.server
Then you will see below iex console. Remember social fund is the name of my application, so remember to use your application name.
iex (socialfund @127 . 0.0 . 1 ) 1 >
Press enter or click to view image in full size
The socialfund@127.0.0.1 is the name of our node with the IP address for the localhost network, thus, you can do this on a wide network by using the right network address.
Get Kamaro Lambert’s stories in your inbox
Join Medium for free to get updates from this writer.
Next, start your Livebook app like below, if you are using Linux. Windows might have a different way of starting livebook server. What is important here is to start your Livebook app.
➜livebook server 
 [ Livebook ] Application running at http: //localhost:8080/?token=n2xxcudq6ffvpcccempjnnfouqvcjmpf
Then, open livebook in the browser and create a new book.
Then, under + Smart select Remote execution.
If you don’t have Kino package installed, your will be prompted to add it.
Click + Add and Restart Press enter or click to view image in full size Install the Kino package is not already present Next, inform Livebook what node to connect to.
In our case socialfund@127.0.0.1 is the node while secret is the cookie.
Go ahead and specify them.
Press enter or click to view image in full size
Press Ctrl + Enter or click Reevaluate button to connect to your applicaton from livebook.
To test whether it worked, I am going to retreive Ash domains from my application configurations with the following command in livebook as if I was to run it in the iex console.
Remember to replace :social_fund with your application name.
Application.get_env( :social_fund , :ash_domain )
You should see below results, with configured Ash domain. Press enter or click to view image in full size Let’s create a module in Livebook and attempt to run it in iex console.
Create the Generator module for the purpose of today’s demonstration. In your livebook, add below code, then reevaluate it.
defmodule Generator do 
 @moduledoc "" " 
 Generate fake data for testing 
 " "" 
 
 @doc "" " 
 Generate random number for testing purpose 
 " "" 
 def number , do: Enum.random([ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]) 
 
 @doc "" " 
 Generate random text for testing purpose 
 " "" 
 def text , do: "Random text because I can write it" 
 end
It should look like below. Press enter or click to view image in full size Once evaluated, you can call it from your iex console as if it was part of your existing application. Press enter or click to view image in full size This makes it much easier to test your code and ideas before writing them in your application. The same way you write module in your application codebase, you can write codes in Livebook and remotely execute it and vis versa.
This approach works for any Elixir application in a node.
Not only can you write simple applications, I have been able to write liveviews, complex component, even APIs in livebook before I ported them to the my application codebase. This empowered me to do quick tests and validate my ideas without feeling like pulling my hair.
What is even more interesting is that you can do this with your Elixir application application hosted anywhere on internet as network permits and it will just work fine.
Let me know in the comment below if you’ve used livebook to code.
