-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
message.ex
72 lines (56 loc) · 2.17 KB
/
message.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
defmodule Phoenix.Socket.Message do
@moduledoc """
Defines a message dispatched over transport to channels and vice-versa.
The message format requires the following keys:
* `:topic` - The string topic or topic:subtopic pair namespace, for
example "messages", "messages:123"
* `:event`- The string event name, for example "phx_join"
* `:payload` - The message payload
* `:ref` - The unique string ref
* `:join_ref` - The unique string ref when joining
"""
@type t :: %Phoenix.Socket.Message{}
defstruct topic: nil, event: nil, payload: nil, ref: nil, join_ref: nil
@doc """
Converts a map with string keys into a message struct.
Raises `Phoenix.Socket.InvalidMessageError` if not valid.
"""
def from_map!(map) when is_map(map) do
try do
%Phoenix.Socket.Message{
topic: Map.fetch!(map, "topic"),
event: Map.fetch!(map, "event"),
payload: Map.fetch!(map, "payload"),
ref: Map.fetch!(map, "ref"),
join_ref: Map.get(map, "join_ref")
}
rescue
err in [KeyError] ->
raise Phoenix.Socket.InvalidMessageError, "missing key #{inspect(err.key)}"
end
end
end
defmodule Phoenix.Socket.Reply do
@moduledoc """
Defines a reply sent from channels to transports.
The message format requires the following keys:
* `:topic` - The string topic or topic:subtopic pair namespace, for example "messages", "messages:123"
* `:status` - The reply status as an atom
* `:payload` - The reply payload
* `:ref` - The unique string ref
* `:join_ref` - The unique string ref when joining
"""
@type t :: %Phoenix.Socket.Reply{}
defstruct topic: nil, status: nil, payload: nil, ref: nil, join_ref: nil
end
defmodule Phoenix.Socket.Broadcast do
@moduledoc """
Defines a message sent from pubsub to channels and vice-versa.
The message format requires the following keys:
* `:topic` - The string topic or topic:subtopic pair namespace, for example "messages", "messages:123"
* `:event`- The string event name, for example "phx_join"
* `:payload` - The message payload
"""
@type t :: %Phoenix.Socket.Broadcast{}
defstruct topic: nil, event: nil, payload: nil
end