achenet
How to use GenServer with Bandit?
Hello ![]()
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 ![]()
Marked As Solved
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.
Also Liked
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.
achenet
Thank you for the replies ^^
I’ll take the time to re-read the doc more carefully.







