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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews