Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Livebook Plug

livebook/data/leruaite.livemd

Livebook Plug

What is livebook ?

Livebook is an amazing tools in elixir, your purpose is to create interactive documents and also write, execute and explain code elixir in document like a jupyter notebook for python, this document could share with other developers for interactive together, the livebook supports tables, graphics, text formating and other ways to visualization data, now that we explain a little bit about what is livebook we gonna think one application elixir that is running in other host, wouldn’t it be pretty good if we could execute this application in that livebook ? this is possible if we connecting node that livebook in node that application.

What is node in Elixir ?

Elixir code after compiled it run in the Erlang Virtual Machine (BEAM), elixir takes advantage of BEAM features. That said, node in erlang is a running instance of Erlang runtime environment that enables creation and commnunication of processes in a distributed system, for identify the current node in a distributed system, execute node.self you could see more about node in docs

Node.self()
:broker@broker

To illustrate, I did a simple select in a context called Persons created in a phoenix application running on another host other than the livebook.

Broker.Repo.get!(Broker.Persons.Person, 2)
%Broker.Persons.Person{
  __meta__: #Ecto.Schema.Metadata<:loaded, "persons">,
  id: 2,
  description: "software engineer",
  name: "Ricardo Pádua",
  inserted_at: ~N[2023-10-25 18:04:25],
  updated_at: ~N[2023-10-25 18:04:25]
}

is important understand that if not exist node created with name defined to host, the connection will failure.

version: "3.8"
services:
  broker:
    container_name: broker
    build: ./broker/
    ports:
      - "5000:5000"
    volumes:
      - ./broker:/app
    hostname: broker
    networks:
      - "live-net"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_HOST: livedb
      ERLANG_COOKIE: "leruaite_secret"
  livebook:
    image: 'livebook/livebook:0.6.3' 
    container_name: livebook
    hostname: livebook
    ports:
      - '8432:8432'
    networks:
      - "live-net"
    environment:
      LIVEBOOK_COOKIE: "leruaite_secret"
      LIVEBOOK_TOKEN_ENABLED: "true"
      LIVEBOOK_PASSWORD: "leruaitepassword"
      LIVEBOOK_DEFAULT_RUNTIME: "attached:broker@broker:leruaite_secret"
      LIVEBOOK_PORT: 8432
    restart: always
    volumes:
      - './livebook/data:/data'

  db:
    image: 'postgres:13.0-alpine' 
    container_name: livedb
    hostname: livedb
    ports:
      - '5432:5432'
    networks:
      - "live-net"
    restart: always
    environment:
      POSTGRES_HOST: livedb
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
      POSTGRES_DB: broker
      PGDATA: /tmp
    volumes:
      - './postgres/data:/var/lib/postgresql/data'


networks:
  live-net:
    name: live-net
    driver: bridge
    ```
FROM elixir:1.13.4

RUN mix local.hex --force \
  && mix archive.install --force hex phx_new 1.6.11 \
  && apt-get update \
  && curl -sL https://deb.nodesource.com/setup_16.x | bash \
  && apt-get install -y apt-utils \
  && apt-get install -y nodejs \
  && apt-get install -y build-essential \
  && apt-get install -y inotify-tools \
  && mix local.rebar --force

ENV APP_HOME /app
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME

EXPOSE 5000

CMD ["mix", "phx.server"]
```
docker-compose up --build -d
docker exec -it broker iex --name broker@broker --cookie leruaite_secret -S mix

Note that this configuration is for example only and security adjustments must be made so that it is viable to run in secure environments. When possible, read the livebook documentation and always place your environment variables in an .env or vault.

you can also auto save your livebook on repo.