How to iterate over a Presence map?

Hi,

I need to somehow iterate over the entries in the map returned by Presence.list/1. This is what the map looks like on my end.

%{
  "user:1" => %{
metas: [
  %{initiator: 2, phx_ref: "tL574g0pn6w=", user_id: 1, username: "admin"}
]
  },
  "user:3" => %{
metas: [
  %{initiator: 1, phx_ref: "7Qvfk3qEksE=", user_id: 3, username: "bob"}
]
  }
}

What I need is to iterate over all of them and assign each with a initiator based on the order in which they appear in the map (ex: 1,2,3,4).

You can iterate a map with Enum.map for example but note that maps are not ordered so it is not guaranteed that you get any specific order for the users.

Example:

iex(1)> Enum.map(%{"a" => 1, "b" => 2}, fn {k, v} -> {k, v + 1} end) |> Enum.into(%{})
%{"a" => 2, "b" => 3}
1 Like

So Enum.map(presences, fn{k, v} -> {k, Enum.map(v, fn{k, v} -> {v.initiator} } ) to iterate over the initiator value, but how do I update it? And btw, I don’t need any particular order just to have each user.initiator be a value from 1-5(no number repeating).

Unsure where you want the initiator value, in the metas list or somewhere else? What do you want a single user map to look like?

1 Like

Just like the one in the first post but with the initiator value updated to be one of 1…5.

This kind of sounds like an XY problem (I don’t understand why you need to do this), but here’s something:

updated = for {{user, val}, i} <- Enum.with_index(your_data) do
  meta =
    hd(val.metas)
    |> put_in([:initiator], i + 1)

  updated_val = put_in(val[:metas], meta)
  {user, updated_val}
end

updated_map = Enum.into(updated, %{})
1 Like

I need it for a webrtc signaling thing I’m developing. What I need precisely is a list of users with scaling numbers assigned to them in order to figure out which peers are supposed to initiate connections with which peers.

I’m not sure why but the updated_map when used in push socket, "presence_state", Presence.list(socket) in place of Presence.list(socket) causes in error in the front end.

Edit 1:

I figured out where the issue was. The function you wrote changes the metas from a list to a map:

This is the result of IO.inspect Presence.list(socket)

%{
  "user:1" => %{
    metas: [
      %{initiator: 1, phx_ref: "7jfjsPHo5nI=", user_id: 1, username: "admin"}
    ]
  },
  "user:2" => %{
    metas: [
      %{initiator: 1, phx_ref: "kbychxZoLLQ=", user_id: 2, username: "test"}
    ]
  },
  "user:3" => %{
    metas: [
      %{initiator: 1, phx_ref: "/3BoMir66Xs=", user_id: 3, username: "bob"}
    ]
  }
}

This is the result of IO.inspect updated_map

%{
  "user:1" => %{
    metas: %{
      initiator: 1,
      phx_ref: "7jfjsPHo5nI=",
      user_id: 1,
      username: "admin"
    }
  },
  "user:2" => %{
    metas: %{
      initiator: 2,
      phx_ref: "kbychxZoLLQ=",
      user_id: 2,
      username: "test"
    }
  },
  "user:3" => %{
    metas: %{initiator: 3, phx_ref: "/3BoMir66Xs=", user_id: 3, username: "bob"}
  }
}

It seems I made a mistake, I replaced the whole metas list with the first element in the list. The error is in updated_val = put_in(val[:metas], meta). Instead of just meta there, we should use [meta | tl(val.metas)] so it is a proper list again.

Note that I am assuming it is the first element in the metas list that you need to change. If it’s some other element, this may not work. Which makes me think there is some better API for this than modifying the structures directly.

1 Like

Thank you this works :slight_smile:

Why do you think I asked this question here?