denvaar

denvaar

Help me decide between Phoenix Presence and a custom solution

I am trying to decide if Phoenix Presence is right for my application, or if I should use my own custom solution to track “players”. Let me explain what I’m working on:

I am making an online multiplayer game. I am using the Phaser Javascript library client side, and I will use Phoenix Channels for the backend to facilitate the multiplayer, realtime features that I’d like to have. Consider these basic features:

  • When a new player logs in to the game, their character is now “online” for others to see and interact with.
  • When a player leaves the . game, their character is now “offline”
  • A player can click around the map to move their player, and the changes should be reflected for all online users as well.

I can see how Presence can handle the first two features easily, but the third one, I’m not so sure the more I look into it. For example, to try and track the player movements, I am doing this:

Presence.update(socket, "player:#{player_id}", %{
    id: player_id,
    x: x,
    y: y
})

The client application will receive this update, but there’s not a good way to determine what the event was, it just returns the entire payload of data. I suppose I could include another key-value pair in the update, which signifies the action, eg. action: "move" and then parse that out on the client side and let the game know what to do with it.

Maybe it would be easier in this case to create a GenServer and use my own Phoenix Channel events to communicate the state of the app. I could pass a “player_id” from the client to the server, and create a GenServer for each active player, which would keep track of their state while they’re logged in and playing. It could also handle saving to the database periodically, or when they log out of the game.

I initially thought Presence would be good for this, but I am having second thoughts. Most of the examples I’ve seen is a chat room, and displaying a list of online users. In this case it’s fine to wipe out the entire list of users each time an event occurs and replace it with a new sync’d state. However, in this game I’m working on, I need to handle individual actions. Would love some opinions and thoughts about this. Thanks!

Most Liked

kokolegorille

kokolegorille

It’s probably better if You dedicate a process per game, which will register/unregister players.

The idea is to be able to switch interface easily, it should be possible to switch from Phoenix to a Text Based client. Thus, it’s not recommended to have your game depend on Presence…

This is the way they do in…

  • Island game in Functional Web development with Elixir, by @lance
  • Multi player Bingo game
xlphs

xlphs

Your 3rd feature sounds like a feature I did which is real time user privilege change, eg you want a ban to take effect immediately.

I don’t use Phoenix, only Phoenix.PubSub to broadcast changes, and GenServer to track user state/logic/etc. Each change is just a JSON message sent over websocket.

axelson

axelson

Scenic Core Team

You may want to look at the implementation of ExVenture since it solves many of the same problems. There’s a number of good posts about it on this forum: Search results for 'exventure' - Elixir Programming Language Forum

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement