JsonKody

JsonKody

Hello,

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!

Showing Posts 1 to 6

sodapopcan

sodapopcan

Hello and welcome to the forums!

I was typing up an answer to this earlier and then it disappeared! So, here it is:

The short answer is: yes, very-well suited. Almost tailor-made!

Here’s a talk and repo for implementing King of Tokyo (a multiplayer board game, if you are unfamiliar) in LiveView.

a8t

a8t

I don’t know how threejs works but have you looked at Phoenix Channels, which is part of what Liveview is written on top of

kokolegorille

kokolegorille

You should think of channels as bidirectional super controllers :slight_smile:

brightball

brightball

I saw a talk about Three.JS a couple of weeks ago. Really cool technology!

Keep us posted on how this progresses.

mindok

mindok

Hi @JsonKody, and welcome!

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:

  1. A client hook - the javascript that manages the Threejs scene and receives user input.
    See JavaScript interoperability — Phoenix LiveView v1.2.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.

  1. 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)

The LiveView can send events to the client hook using push_event (see JavaScript interoperability — Phoenix LiveView v1.2.5) and handles event from the client side using handle_event.

  1. To get the multi-user thing going, the LiveViews need to talk to each other. Probably the easiest way is to use PubSub with a topic per game. When the LiveView mounts, it can subscribe to the PubSub topic related to the game. This will ensure that LiveView process receives the game events. In the project @sodapopcan linked, you can see it happening here: king_of_tokyo/lib/king_of_tokyo_web/live/game_live.ex at 27f992f28e5be80125f36aed79d95812c568b1c0 · dkarter/king_of_tokyo · GitHub. The events are handled in the various handle_info functions in the same file.

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: king_of_tokyo/lib/king_of_tokyo/game_server.ex at 27f992f28e5be80125f36aed79d95812c568b1c0 · dkarter/king_of_tokyo · GitHub - 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.

Good luck!

JsonKody

JsonKody OP

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)

— 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