amjr

amjr

Hi,

I’m currently diving into world of websockets via Phoenix channels. Absolutely awesome stuff.

The thing I’m wondering is Phoenix presence. Almost all of the examples are chat-examples however my use-case is not a chat but rather a game.

Usually everybody handles the presence state diff-messages in Javascript. For example:

room.on("presence_diff", diff => {
  presences = Presence.syncDiff(presences, diff)
  render(presences)
})

given this channel implementation:

defmodule PhoenixChat.RoomChannel do
  use PhoenixChat.Web, :channel
  alias PhoenixChat.Presence

  def join("room:lobby", _, socket) do
    send self(), :after_join
    {:ok, socket}
  end

  def handle_info(:after_join, socket) do
    Presence.track(socket, socket.assigns.user, %{
      online_at: :os.system_time(:milli_seconds)
    })
    push socket, "presence_state", Presence.list(socket)
    {:noreply, socket}
  end
end

How can I suppress the sending of presence_diffs to clients? As I don’t want to expose the presence information to connected clients but keep it only on the server side.

Showing Posts 5 to 1

chrismccord

chrismccord

Creator of Phoenix

intercepting is the way to go to easily filter out the presence_diff events

amjr

amjr OP

Great. This helps me forward. Thanks!

By the way. Gotta love the Elixir/Phoenix community. You guys rock!

OvermindDL1

OvermindDL1

That is precisely it. :slight_smile:

Personally I would have a few processing processes that handle the messages of what is happening, and ‘they’ would then send messages to topics to send updates to clients, and the clients would connect to as many topics as they need for the data they want to receive. :slight_smile:

amjr

amjr OP

Intercepting would be something like this?

intercept ["presence_diff"]

def handle_out("presence_diff", msg, socket) do
    {:noreply, socket}
end

The target is to use the channel for sending in-game data and event to clients and utilize the presence internally on the server-side. Client side would only connect the websocket (swift / iOS, Android) and get updates on the game events.
Clients wouldn’t get updates on who is online and who is not (prevent players for example attacking players who go offline in the hopes of catching them off-guard)

OvermindDL1

OvermindDL1

Two ways, you can also PubSub subscribe to a new process that does all the actual processing with the messages then posts to the client as needed, this would also let clients share a ‘processing’ actor. Or you can hook the ‘outgoing’ messages in Phoenix Channels (detailed in the docs) for the outgoing message of “presence_diff” then do what you wish, such as pushing it out or canceling it or whatever. :slight_smile:

For note, if the client should get the pubsub messages then they should get them all or will desync.

If the client should get none then it should be handled by another process.

The client should never just get ‘some’, what you probably want here is an intermediary process as well to coalesce the updates.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews