I’m relatively new to Phoenix, having just started the Pragmatic Studio LiveView course. Concurrently, I’m learning Three.js via the Threejs-Journey course. My primary background is in JS/TS/Vue development.
I’m interested in creating a straightforward “memory card” game. I envision it as an online multiplayer game, where two players can connect or create a “game room” that displays the 3D memory card game. My idea is to utilize Phoenix as the game server, primarily responsible for tracking the positions of the cards.
My main question is: would LiveView be suitable for such a project? While I can conceptualize the connection between a Vue or React app and making REST calls on every card flip, I’m finding it challenging to visualize how I’d integrate the game with LiveView, especially in the context of websockets.
Any guidance or insights you can provide would be greatly appreciated!
LiveView would be awesome for this project and simplifies a lot of the plumbing over raw Phoenix Channels. I have embedded Babylon JS (Threejs alternative), and Konvajs (2D rather than 3D) and they work great. Here are a few of the ingredients you need:
A client hook - the javascript that manages the Threejs scene and receives user input.
See JavaScript interoperability — Phoenix LiveView v0.19.5
This can have event handlers to handle events pushed from the server (i.e. the server-side LiveView code) (see handleEvent in the linked doc. It can also push information back to the server LiveView using pushEvent or pushEventTo.
The fact that there is a long-lived server process on the other end of the conversation simplifies state management enormously.
The LiveView itself. In the LiveView template you will need to install the client hook somewhere. Search phx-hook for instructions. Note that if threejs is doing DOM manipulation, you will need to mark the DOM node that it attaches to in a special way to prevent patching from the server (use phx-update="ignore" (see https://hexdocs.pm/phoenix_live_view/dom-patching.html)
To notify other game participants in the same game, you would use Phones.PubSub to broadcast events to the game topic. It’s a bit hard to follow in the linked project as they spin up a backing process per game in the GameServer module, but there’s a call here: https://github.com/dkarter/king_of_tokyo/blob/27f992f28e5be80125f36aed79d95812c568b1c0/lib/king_of_tokyo/game_server.ex#L258 - if you backtrack where that’s called from (e.g. the broadcast_... functions) you can see how the payload is constructed and how that is matched in the handle_infos in the LiveView.
Wow! Thanks for such good answers … this feels like polar opposite of Stack Overflow ( ͡° ͜ʖ ͡°)
Elixir has amazing comunity <3
I’ll look at all those links after weekend, thank you again.
PS: ok I’ll share it when it’s done but it would be so much work (I’ve some other projects before it - they will help me become proficient with Elixir/Phoenix/Liveview hopefully)