-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathPresence.elm
305 lines (235 loc) · 8.06 KB
/
Presence.elm
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
module Presence exposing (..)
import Html as Html
import Html exposing (..)
import Html.Attributes exposing (value, placeholder, class)
import Html.Events exposing (onInput, onClick, onSubmit)
import Phoenix.Socket
import Phoenix.Channel
import Phoenix.Push
import Phoenix.Presence exposing (PresenceState, syncState, syncDiff, presenceStateDecoder, presenceDiffDecoder)
import Json.Encode as JE
import Json.Decode as JD
import Debug
import Dict exposing (Dict)
-- Our model will track a list of messages and the text for our new message to
-- send. We only support chatting in a single channel for now.
type alias User =
{ name : String
}
type alias UserPresence =
{ online_at : String
, device : String
}
type alias ChatMessage =
{ user : String
, body : String
}
type alias Model =
{ newMessage : String
, messages : List ChatMessage
, username : String
, users : List User
, phxSocket : Maybe (Phoenix.Socket.Socket Msg)
, phxPresences : PresenceState UserPresence
}
type Msg
= SetNewMessage String
| JoinChannel
| PhoenixMsg (Phoenix.Socket.Msg Msg)
| SendMessage
| ReceiveChatMessage JE.Value
| SetUsername String
| ConnectSocket
| HandlePresenceState JE.Value
| HandlePresenceDiff JE.Value
initialModel : Model
initialModel =
{ newMessage = ""
, messages = []
, username = ""
, users = []
, phxSocket = Nothing
, phxPresences = Dict.empty
}
socketServer : String -> String
socketServer username =
"ws://localhost:4000/socket/websocket?username=" ++ username
initPhxSocket : String -> Phoenix.Socket.Socket Msg
initPhxSocket username =
Phoenix.Socket.init (socketServer username)
|> Phoenix.Socket.withDebug
|> Phoenix.Socket.on "new:msg" "room:lobby" ReceiveChatMessage
|> Phoenix.Socket.on "presence_state" "room:lobby" HandlePresenceState
|> Phoenix.Socket.on "presence_diff" "room:lobby" HandlePresenceDiff
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
SetNewMessage string ->
{ model | newMessage = string } ! []
JoinChannel ->
case model.phxSocket of
Nothing ->
model ! []
Just modelPhxSocket ->
let
channel =
Phoenix.Channel.init "room:lobby"
( phxSocket, phxCmd ) =
Phoenix.Socket.join channel modelPhxSocket
in
( { model | phxSocket = Just phxSocket }
, Cmd.map PhoenixMsg phxCmd
)
PhoenixMsg msg ->
case model.phxSocket of
Nothing ->
model ! []
Just modelPhxSocket ->
let
( phxSocket, phxCmd ) =
Phoenix.Socket.update msg modelPhxSocket
in
( { model | phxSocket = Just phxSocket }
, Cmd.map PhoenixMsg phxCmd
)
SendMessage ->
case model.phxSocket of
Nothing ->
model ! []
Just modelPhxSocket ->
let
payload =
(JE.object [ ( "body", JE.string model.newMessage ) ])
pushPrime =
Phoenix.Push.init "new:msg" "room:lobby"
|> Phoenix.Push.withPayload payload
( phxSocket, phxCmd ) =
Phoenix.Socket.push pushPrime modelPhxSocket
in
( { model
| newMessage = ""
, phxSocket = Just phxSocket
}
, Cmd.map PhoenixMsg phxCmd
)
ReceiveChatMessage raw ->
case JD.decodeValue chatMessageDecoder raw of
Ok chatMessage ->
{ model | messages = model.messages ++ [ chatMessage ] } ! []
Err error ->
model ! []
SetUsername username ->
{ model | username = username } ! []
ConnectSocket ->
{ model | phxSocket = Just (initPhxSocket model.username) } ! []
HandlePresenceState raw ->
case JD.decodeValue (presenceStateDecoder userPresenceDecoder) raw of
Ok presenceState ->
let
newPresenceState =
model.phxPresences |> syncState presenceState
users =
Dict.keys presenceState
|> List.map User
in
{ model | users = users, phxPresences = newPresenceState } ! []
Err error ->
let
_ =
Debug.log "Error" error
in
model ! []
HandlePresenceDiff raw ->
case JD.decodeValue (presenceDiffDecoder userPresenceDecoder) raw of
Ok presenceDiff ->
let
newPresenceState =
model.phxPresences |> syncDiff presenceDiff
users =
Dict.keys newPresenceState
|> List.map User
in
{ model | users = users, phxPresences = newPresenceState } ! []
Err error ->
let
_ =
Debug.log "Error" error
in
model ! []
chatMessageDecoder : JD.Decoder ChatMessage
chatMessageDecoder =
JD.map2 ChatMessage
(JD.oneOf
[ (JD.field "user" JD.string)
, JD.succeed "anonymous"
]
)
(JD.field "body" JD.string)
userPresenceDecoder : JD.Decoder UserPresence
userPresenceDecoder =
JD.map2 UserPresence
(JD.field "online_at" JD.string)
(JD.field "device" JD.string)
viewMessage : ChatMessage -> Html Msg
viewMessage message =
div [ class "message" ]
[ span [ class "user" ] [ text (message.user ++ ": ") ]
, span [ class "body" ] [ text message.body ]
]
lobbyManagementView : Html Msg
lobbyManagementView =
button [ onClick JoinChannel ] [ text "Join lobby" ]
messageListView : Model -> Html Msg
messageListView model =
div [ class "messages" ]
(List.map viewMessage model.messages)
messageInputView : Model -> Html Msg
messageInputView model =
form [ onSubmit SendMessage ]
[ input [ placeholder "Message...", onInput SetNewMessage, value model.newMessage ] [] ]
userListView : Model -> Html Msg
userListView model =
ul [ class "users" ]
(List.map userView model.users)
userView : User -> Html Msg
userView user =
li []
[ text user.name
]
chatInterfaceView : Model -> Html Msg
chatInterfaceView model =
div []
[ lobbyManagementView
, messageListView model
, messageInputView model
, userListView model
]
setUsernameView : Html Msg
setUsernameView =
form [ onSubmit ConnectSocket ]
[ input [ onInput SetUsername, placeholder "Enter a username" ] [] ]
view : Model -> Html Msg
view model =
case model.phxSocket of
Nothing ->
setUsernameView
_ ->
chatInterfaceView model
main : Program Never Model Msg
main =
Html.program
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}
subscriptions : Model -> Sub Msg
subscriptions model =
case model.phxSocket of
Nothing ->
Sub.none
Just phxSocket ->
Phoenix.Socket.listen phxSocket PhoenixMsg
init : ( Model, Cmd Msg )
init =
( initialModel, Cmd.none )