Skip to content

Commit 22225e3

Browse files
Merge pull request #3 from elixirschool/mts/add_presence
Add Phoenix Presence
2 parents f8daaee + 6360058 commit 22225e3

File tree

11 files changed

+2764
-7461
lines changed

11 files changed

+2764
-7461
lines changed

assets/js/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ import "phoenix_html"
1414
// Import local files
1515
//
1616
// Local files can be imported directly using relative paths, for example:
17-
// import socket from "./socket"
17+
import socket from "./socket"

assets/js/socket.js

Lines changed: 18 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,25 @@
1-
// NOTE: The contents of this file will only be executed if
2-
// you uncomment its entry in "assets/js/app.js".
1+
import {Socket, Presence} from 'phoenix'
32

4-
// To use Phoenix channels, the first step is to import Socket,
5-
// and connect at the socket path in "lib/web/endpoint.ex".
6-
//
7-
// Pass the token on params as below. Or remove it
8-
// from the params if you are not using authentication.
9-
import {Socket} from "phoenix"
3+
const socket = new Socket('/socket', {params: {username: window.pointingParty.username}})
4+
socket.connect()
105

11-
let socket = new Socket("/socket", {params: {token: window.userToken}})
6+
const channel = socket.channel('room:lobby', {})
7+
const presence = new Presence(channel)
128

13-
// When you connect, you'll often need to authenticate the client.
14-
// For example, imagine you have an authentication plug, `MyAuth`,
15-
// which authenticates the session and assigns a `:current_user`.
16-
// If the current user exists you can assign the user's token in
17-
// the connection for use in the layout.
18-
//
19-
// In your "lib/web/router.ex":
20-
//
21-
// pipeline :browser do
22-
// ...
23-
// plug MyAuth
24-
// plug :put_user_token
25-
// end
26-
//
27-
// defp put_user_token(conn, _) do
28-
// if current_user = conn.assigns[:current_user] do
29-
// token = Phoenix.Token.sign(conn, "user socket", current_user.id)
30-
// assign(conn, :user_token, token)
31-
// else
32-
// conn
33-
// end
34-
// end
35-
//
36-
// Now you need to pass this token to JavaScript. You can do so
37-
// inside a script tag in "lib/web/templates/layout/app.html.eex":
38-
//
39-
// <script>window.userToken = "<%= assigns[:user_token] %>";</script>
40-
//
41-
// You will need to verify the user token in the "connect/3" function
42-
// in "lib/web/channels/user_socket.ex":
43-
//
44-
// def connect(%{"token" => token}, socket, _connect_info) do
45-
// # max_age: 1209600 is equivalent to two weeks in seconds
46-
// case Phoenix.Token.verify(socket, "user socket", token, max_age: 1209600) do
47-
// {:ok, user_id} ->
48-
// {:ok, assign(socket, :user, user_id)}
49-
// {:error, reason} ->
50-
// :error
51-
// end
52-
// end
53-
//
54-
// Finally, connect to the socket:
55-
socket.connect()
9+
presence.onSync(() => {
10+
const users = document.querySelector('.users')
11+
users.innerHTML = ''
12+
13+
presence.list((id, _) => {
14+
const user = document.createElement('li')
15+
user.setAttribute('class', id)
16+
user.appendChild(document.createTextNode(id))
17+
users.appendChild(user)
18+
})
19+
})
5620

57-
// Now that you are connected, you can join channels with a topic:
58-
let channel = socket.channel("topic:subtopic", {})
5921
channel.join()
60-
.receive("ok", resp => { console.log("Joined successfully", resp) })
61-
.receive("error", resp => { console.log("Unable to join", resp) })
22+
.receive('ok', resp => { console.log('Joined successfully', resp) })
23+
.receive('error', resp => { console.log('Unable to join', resp) })
6224

6325
export default socket

0 commit comments

Comments
 (0)