polyglot

polyglot

Questions about a game I created (handling multiple concurrent users and is there a way to hook into the server using :observer.start?)

I have tried creating a simple application Games where a http server is running and users can interact with it and play games. I have also embedded a demo in the README to see how to play it. I have missed adding tests and error handling. I have few questions regarding it.

Questions:

  1. Is this a correct way to handle multiple concurrent users where the application accepts a client connection and spawns a new process to serve it. The user can then play a game and the server creates a new task for it and awaits for a fixed time.
game = Task.async(fn -> RockPaperScissors.play(id, client_socket) end)
Task.await(game, 1 * 60 * 1000)
  1. I have added :observer, :wx, :runtime_tools in extra_applications:. Since I am starting a http server the terminal blocks. Is there a way to hook into the server using :observer.start() or other means and see how many users are currently playing?

I would be very grateful if you could provide some feedback. Thanks.

Most Liked

RudManusachi

RudManusachi

Is this a correct way to handle multiple concurrent users where the application accepts a client connection and spawns a new process to serve it.

Absolutely! That’s the way to go! For example, in a nutshell, Phoenix Channels and LiveViews hold a process for each stateful web socket connection.

Since I am starting a http server the terminal blocks. Is there a way to hook into the server using :observer.start() or other means and see how many users are currently playing?

The simplest, in your scenario, probably would be somewhere in application.ex in start callback just call :observer.start() so once you start the app - it first would start the observer and then start the server itself (though you don’t want that by default for users, maybe via some ENV variable can be configured?)

However, some notes for the reference:

  1. as mentioned in docs:

    Escripts should be used as a mechanism to share scripts between developers and not as a deployment mechanism. For running live systems, consider using mix run or building releases. See the Application module for more information on systems life-cycles.

    So one alternative option would be if running the app with mix (either release or just mix run) we could start it as named node, for example:

    iex --sname server -S mix
    

    (here we start our mix project in a Node named “server”)
    and now from another terminal we could attach iex as a remote shell with

    iex --remsh server --sname observer
    

    (Here we start a node named “observer” and attach remsh (a remote shell) to the node “server”)

    As of now, it would require to manually start the HttpServer..

    Games.HttpServer.start(9000)
    

    so once we start the server - it will block the shell, just as with your escript. But we have another shell attached to the server! where we could run :observer.start()! or whatever we want!
    (though it would probably make sense to have that Games.HttpServer.start to be part of the main Application supervision tree.. so it starts automatically and doesn’t block your shell)

  2. In your scenario I’d recommend to spawn a “supervised” process either through DynamicSupervisor or Task.Supervisor, instead of just bare bone spawn(fn -> ... end).

    We encourage developers to rely on supervised tasks as much as possible. Supervised tasks improve the visibility of how many tasks are running at a given moment and enable a variety of patterns that give you explicit control on how to handle the results, errors, and timeouts.

    Task — Elixir v1.20.2

    Plus, processes that are parts of a supervision tree are not only more flexible with tree are nicely shown in the “applications” tab in :observer :slightly_smiling_face:

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement