Hi,
Using:
- phoenix 1.3.0-rc.2 (Hex package) (mix)
- ecto 2.1.4 (Hex package) (mix)
- phoenix_ecto 3.2.3 (Hex package) (mix)
I use a channel with a join
function like this:
def join("positions:" <> tenant, payload, socket) do
positions = Accounts.list_positions(tenant)
{:ok, positions, socket}
end
positions
is just a list of Position
.
It seems to me there is no point to add a surrounding object to the list in a ok
response as we know what to expect (this is also a common practice in GET
or POST
HTTP requests/responses).
This works correctly, the list of positions is serialized and sent in the response:
{"status":"ok","response":[]},"event":"phx_reply"}
However when I want to broadcast the list of positions from a controller:
positions = Accounts.list_positions(tenant)
Endpoint.broadcast("positions:" <> tenant, "positions", positions)
This fails with the following error:
** (ArgumentError) topic and event must be strings, message must be a map
because broadcast/3
which uses Server.broadcast
only accepts maps for the payload:
def broadcast(pubsub_server, topic, event, payload)
when is_binary(topic) and is_binary(event) and is_map(payload) do
...
end
def broadcast(_, _, _, _), do: raise_invalid_message()
So is there another way to broadcast directly a list like in the join
response ? Does it makes sense or did I miss something ?
Thanks