Skip to content

Commit 4691731

Browse files
committed
Updating socket auth chapter
1 parent 8a8b14b commit 4691731

File tree

3 files changed

+52
-66
lines changed

3 files changed

+52
-66
lines changed

manuscript/Rewrites.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
- [x] adding_interaction.md
1717
- [x] displaying_game_data.md
1818
- [x] handling_game_states.md
19-
- [x] phoenix_channel_setup.md
19+
- [ ] phoenix_channel_setup.md
2020
- [ ] syncing_score_data.md
2121
- [ ] socket_authentication.md
2222
- [x] whats_next.md
2323
- [x] outline.md
2424
- [x] appendix.md
2525
- [x] contact.md
2626
- [x] run proselint
27+
- [ ] fix channel issues in last 3 chapters
2728
- [ ] update Elm API chapter for new Http module API
2829
- [ ] test all content from beginning for new sample
2930
- [ ] replace old sample app on GitHub with new one

manuscript/socket_authentication.md

Lines changed: 49 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ add a new `<script>` tag **above** the existing one in the `<body>`.
6565

6666
```embedded_elixir
6767
<!-- ... -->
68-
</div> <!-- /container -->
6968
<script>window.userToken = encodeURIComponent("<%= assigns[:user_token] %>");</script>
7069
<script src="<%= static_path(@conn, "/js/app.js") %>"></script>
7170
</body>
@@ -130,58 +129,60 @@ along the `window.userToken`:
130129

131130
```javascript
132131
// Elm
133-
import Elm from "./elm";
132+
import { Elm } from "../elm/src/Main.elm";
134133

135134
const elmContainer = document.querySelector("#elm-container");
136135
const platformer = document.querySelector("#platformer");
137136

138-
if (elmContainer) Elm.Main.embed(elmContainer);
137+
if (elmContainer) {
138+
Elm.Main.init({ node: elmContainer });
139+
}
140+
if (platformer) {
141+
Elm.Games.Platformer.init({
142+
node: platformer,
143+
flags: { token: window.userToken }
144+
});
145+
}
146+
139147
if (platformer) Elm.Platformer.embed(platformer, { token: window.userToken });
140148
```
141149

142150
If you're still running the Phoenix server, you'll notice that the Elm
143-
application for our game no longer works. Let's open our
144-
`assets/elm/Platformer.elm` file and fix things up.
145-
146-
## ProgramWithFlags
151+
application for our game no longer works. Let's open our `Platformer.elm` file
152+
and fix things up.
147153

148-
At the top of our `Platformer.elm` file, we've been using `Html.program` in our
149-
`main` function. We're going to make a slight change to our application by
150-
converting it to `programWithFlags` and adding a new type alias above:
154+
## Working with Flags
151155

152-
```elm
153-
type alias Flags =
154-
{ token : String
155-
}
156+
Flags allow us to send data from JavaScript to Elm when we initialize our
157+
application. When we embed our Elm application on the page with JavaScript,
158+
we're passing along some data that we'll need to use. In this case, we're
159+
sending our `window.userToken` value to the Elm application, where we'll be
160+
able to access it as a `token` string.
156161

162+
In our `Platformer.elm` file, we've previously been ignoring any incoming data
163+
that's passed as an argument to our `init` function:
157164

158-
main : Program Flags Model Msg
159-
main =
160-
Html.programWithFlags
161-
{ init = init
162-
, view = view
163-
, update = update
164-
, subscriptions = subscriptions
165-
}
165+
```elm
166+
init : () -> ( Model, Cmd Msg )
167+
init _ =
168+
( initialModel, Cmd.none )
166169
```
167170

168-
Flags allow JavaScript and Elm to communicate. When we're embedding our Elm
169-
application on the page with JavaScript, we're passing along some data that
170-
we'll need to use. In this case, we're sending our `window.userToken` value
171-
to the Elm application, where we'll be able to access it as a `token` string.
172-
173-
## Configuring Flags
174-
175-
Now, we just need to update our existing functions to work with our `Flags`.
176-
We'll start by updating our `init` function with the following:
171+
Let's create a new `Flags` type and update our `init` function to handle the
172+
incoming data:
177173

178174
```elm
175+
type alias Flags =
176+
{ token : String
177+
}
178+
179179
init : Flags -> ( Model, Cmd Msg )
180180
init flags =
181-
( initialModel flags, Cmd.map PhoenixMsg (initialSocketCommand flags) )
181+
( initialModel flags, Cmd.none )
182182
```
183183

184-
We'll also need to pass our flags through the `initialModel` as well:
184+
Now that we're using flags to initialize our application with external data,
185+
we can update our `initialModel` to pass the data along to our socket:
185186

186187
```elm
187188
initialModel : Flags -> Model
@@ -194,49 +195,33 @@ initialModel flags =
194195
, itemPositionX = 500
195196
, itemPositionY = 300
196197
, itemsCollected = 0
197-
, phxSocket = initialSocketJoin flags
198+
, phxSocket = initialSocket flags
198199
, playerScore = 0
199200
, timeRemaining = 10
200201
}
201202
```
202203

203-
Then, we can update our `initialSocketJoin` and `initialSocketCommand`
204-
function:
205-
206-
```elm
207-
initialSocketJoin : Flags -> Phoenix.Socket.Socket Msg
208-
initialSocketJoin flags =
209-
initialSocket flags
210-
|> Tuple.first
211-
212-
213-
initialSocketCommand : Flags -> Cmd (Phoenix.Socket.Msg Msg)
214-
initialSocketCommand flags =
215-
initialSocket flags
216-
|> Tuple.second
217-
```
218-
219-
Lastly, we update our `initialSocket` function with the key changes. We're
220-
going to take the value from `flags.token`, and append it to the URL for the
221-
WebSocket server.
204+
And we'll update our `initialSocket` function so we can hit a different
205+
endpoint when we have a user token stored in `flags.token`.
222206

223207
```elm
224-
initialSocket : Flags -> ( Phoenix.Socket.Socket Msg, Cmd (Phoenix.Socket.Msg Msg) )
208+
initialSocket : Flags -> Phoenix.Socket.Socket Msg
225209
initialSocket flags =
226210
let
227211
devSocketServer =
228-
"ws://localhost:4000/socket/websocket?token=" ++ flags.token
212+
if String.isEmpty flags.token then
213+
"ws://localhost:4000/socket/websocket"
214+
215+
else
216+
"ws://localhost:4000/socket/websocket"
217+
++ "?token="
218+
++ flags.token
229219
in
230-
Phoenix.Socket.init devSocketServer
231-
|> Phoenix.Socket.withDebug
232-
|> Phoenix.Socket.on "save_score" "score:platformer" SaveScore
233-
|> Phoenix.Socket.on "save_score" "score:platformer" ReceiveScoreChanges
234-
|> Phoenix.Socket.join initialChannel
220+
Phoenix.Socket.init devSocketServer
235221
```
236222

237-
We're done with the updates to our Elm application! Now, when players connect
238-
to the socket, the channel will be able to identify the current user by their
239-
token.
223+
Now, when players connect to the socket, the channel will be able to identify
224+
the current user by their token.
240225

241226
## Finishing the Score Channel
242227

@@ -247,7 +232,7 @@ Update the `handle_in/3` function with the following, which takes the
247232
`player_id` value from `socket.assigns.player_id`:
248233

249234
```elixir
250-
def handle_in("save_score", %{"player_score" => player_score} = payload, socket) do
235+
def handle_in("save_score", %{"player_score" => player_score}, socket) do
251236
payload = %{
252237
player_score: player_score,
253238
game_id: socket.assigns.game_id,

manuscript/syncing_score_data.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ initialSocket =
349349
in
350350
Phoenix.Socket.init devSocketServer
351351
|> Phoenix.Socket.withDebug
352-
|> Phoenix.Socket.on "save_score" "score:platformer" SaveScore
352+
|> Phoenix.Socket.on "save_score" "score:platformer" SaveScoreSuccess
353353
|> Phoenix.Socket.on "save_score" "score:platformer" ReceiveScoreChanges
354354
|> Phoenix.Socket.join initialChannel
355355
```

0 commit comments

Comments
 (0)