ryanwold.net

A civic-minded citizen seeking the singularity

Back to Entries

Using Elixir's GenServers

release
Date: 2021-02-10
Tags: notes elixir genserver

Elixir GenServers are lightweight processes. GenServers provide a common interface for starting, stopping, and passing messages to and from. What you do from there is up to you.

GenServer processes can be adorned with custom behavior. This is done using basic Elixir modules that use GenServer.

Write a public interface for the client using basic methods. Then, write the internal handlers, using handle_cast and handle_call.

Resources

Try it out

```

Start the GenServer and grab its PID (process ID)

{:ok, pid} = ShoppingList.start_link()

Add milk, cheese, and eggs to the list

ShoppingList.add(pid, "milk") ShoppingList.add(pid, "cheese") ShoppingList.add(pid, "eggs")

View the list

ShoppingList.view(pid)

Remove cheese from the list

ShoppingList.remove(pid, "cheese")

View the updated list

ShoppingList.view(pid)

```

By Ryan Wold · © 2021–2026 Ryan Wold

Licensed CC BY-NC 4.0. AI training requires a license — machine-readable terms.

Enjoyed this? — any BSV wallet, HandCash, or plain old dollars.

Leave a tip

Tips go directly to a Bitcoin SV address I control — peer to peer, no platform in between. A few thousand satoshis is a fraction of a cent; it's the gesture that counts.

From any BSV wallet

Scan the QR code with your wallet's camera, or copy the address.

177DwUfSTW8RkFBJzYYcgr1YB8ngbhTYAc

With HandCash or any paymail wallet

Paymail is an email-style address for Bitcoin wallets. Send any amount to afomi@handcash.io

First time using Bitcoin? Yours Wallet is an open-source browser extension wallet, and HandCash is an easy mobile wallet — either takes a minute to set up.

Prefer regular money? Support via OpenCollective.