achenet

achenet

Hello :slight_smile:

I’m struggling to get GenServer working with Bandit.
I’d like to build a simple chat server, with a POST / endpoint which enables the user to add a message, and a GET / endpoint which enables the user to list all existing messages.

This entails having some way to store the messages on the server.
From looking around it seems that GenServer

is the simplest way to this, so I tried using that.
My (very noob) code can be found here:

Basically, in lib/wusup.ex I have my handlers defined, and in lib/wusup/messages.ex I’ve tried to implement the message list following the documentation of the GenServer module.

# lib/wusup.ex
defmodule Wusup do
  use Plug.Router
  require Logger
  require EEx

  EEx.function_from_file(:def, :index, "client/index.html.eex", [:messages, :who])

  plug :match
  plug :dispatch

  get "/" do
    Logger.info("GET /")
    pid = GenServer.whereis(Wusup.Messages)
    messages = Wusup.Messages.list(pid)
    html_string = index(messages, "Ariel")
    Logger.info(html_string)
    put_resp_content_type(conn, "text/html")
    send_resp(conn, 200, html_string)
  end

  post "/" do
    Logger.info("POST /")
    GenServer.call(Messages, {:post, "new message :)"})
    # TODO
  end

  match _ do
    Logger.warning("unknown path: #{conn.request_path}")
    send_resp(conn, 404, "Not found")
  end
end

# lib/wusup/messages.ex
defmodule Wusup.Messages do
  # implement basic GenServer message store
  use GenServer

  # client side
  def start_link(default) do
    GenServer.start_link(__MODULE__, default)
  end

  def list(pid) do
    {:reply, output, _} = GenServer.call(pid, :list)
    output
  end
  

  # server callbacks
  @impl true
  def init(elements) do
    initial_state = String.split(elements, ".", trim: true)
    {:ok, initial_state}
  end

  @impl true
  def handle_call(:list, _from, state) do
    output = state
    {:reply, output, state}
  end

  @impl true
  def handle_cast({:post, message}, state) do
    new_state = [message | state]
    {:noreply, new_state}
  end

end

My lib/wusup/application.ex file, which in theory should be helping the two modules play well together, is

# lib/wusup/application.ex
defmodule Wusup.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  @impl true
  def start(_type, _args) do
    children = [
      {Bandit, plug: Wusup, scheme: :http, port: 4000},
      {Wusup.Messages, "test message 1, test message two, gansta"}
    ]

    opts = [strategy: :one_for_one, name: Wusup.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

However, when I run the code and try to access localhost:4000 with my browser, I get the following error:


15:32:22.486 [error] ** (exit) exited in: GenServer.call(nil, :list, 5000)
    ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
    (elixir 1.17.3) lib/gen_server.ex:1121: GenServer.call/3
    (wusup 0.1.0) lib/wusup/messages.ex:11: Wusup.Messages.list/1
    (wusup 0.1.0) lib/wusup.ex:14: anonymous fn/2 in Wusup.do_match/4
    (wusup 0.1.0) deps/plug/lib/plug/router.ex:246: anonymous fn/4 in Wusup.dispatch/2
    (telemetry 1.3.0) /home/ariel/wd/wusup/deps/telemetry/src/telemetry.erl:324: :telemetry.span/3
    (wusup 0.1.0) deps/plug/lib/plug/router.ex:242: Wusup.dispatch/2
    (wusup 0.1.0) lib/wusup.ex:1: Wusup.plug_builder_call/2
    (bandit 1.8.0) lib/bandit/pipeline.ex:131: Bandit.Pipeline.call_plug!/2


so it seems like I’ve made some basic error with processes here.
How can I fix it?

Also, if there is any documentation (besides the official Elixir docs) or examples/tutorials y’all could recommend, or if y’all have any tips for improving my codebase, please don’t hesitate to let me know :wink:

Showing Posts 1 to 3

derek-zhou

derek-zhou

There are many things wrong with your code, number one being you did not register your GenServer with a name. Name registration is descripbed in the documentation you linked:

My suggestion is not to read more documentation, but to read current documentation well.

garrison

garrison

If you look at the docs for whereis/1 you will see that it returns nil if a registered process is not found with that name.

Because you are calling GenServer.whereis(name) and then using the result in GenServer.call(), you are attempting to call a process named nil. But there is no process named nil (and actually there cannot be, as this is not allowed), hence the error.

As mentioned above the reason is you have not registered the process with a name, and you can read those docs to see how to do that. But, also, you do not need to call whereis() to get the pid because GenServer.call(:some_name, ...) will resolve the name on its own. Actually, you can call send(:some_name, "some message") to send a message directly to a registered name, so there is no resolution needed at all.

achenet

achenet OP

Thank you for the replies ^^

I’ll take the time to re-read the doc more carefully.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews