achenet

achenet

How to use GenServer with Bandit?

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:

Marked As Solved

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.

Also Liked

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.

achenet

achenet

Thank you for the replies ^^

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

Where Next?

Popular in Questions Top

belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

We're in Beta

About us Mission Statement