aboutsummaryrefslogtreecommitdiff
path: root/assets/js
diff options
context:
space:
mode:
authorGuillermo Ramos2020-02-04 13:49:53 +0100
committerGuillermo Ramos2020-02-04 14:20:23 +0100
commit1566a2fcba0676cf54dde2b04702a320d9f1edcd (patch)
tree1d6b378f2d5192fbc3f9446ba9a0cd6e8af2c4c7 /assets/js
downloadwaev-1566a2fcba0676cf54dde2b04702a320d9f1edcd.tar.gz
Initial commit
Diffstat (limited to 'assets/js')
-rw-r--r--assets/js/app.js17
-rw-r--r--assets/js/socket.js63
2 files changed, 80 insertions, 0 deletions
diff --git a/assets/js/app.js b/assets/js/app.js
new file mode 100644
index 0000000..8a5d386
--- /dev/null
+++ b/assets/js/app.js
@@ -0,0 +1,17 @@
+// We need to import the CSS so that webpack will load it.
+// The MiniCssExtractPlugin is used to separate it out into
+// its own CSS file.
+import css from "../css/app.css"
+
+// webpack automatically bundles all modules in your
+// entry points. Those entry points can be configured
+// in "webpack.config.js".
+//
+// Import dependencies
+//
+import "phoenix_html"
+
+// Import local files
+//
+// Local files can be imported directly using relative paths, for example:
+// import socket from "./socket"
diff --git a/assets/js/socket.js b/assets/js/socket.js
new file mode 100644
index 0000000..09929ab
--- /dev/null
+++ b/assets/js/socket.js
@@ -0,0 +1,63 @@
+// NOTE: The contents of this file will only be executed if
+// you uncomment its entry in "assets/js/app.js".
+
+// To use Phoenix channels, the first step is to import Socket,
+// and connect at the socket path in "lib/web/endpoint.ex".
+//
+// Pass the token on params as below. Or remove it
+// from the params if you are not using authentication.
+import {Socket} from "phoenix"
+
+let socket = new Socket("/socket", {params: {token: window.userToken}})
+
+// When you connect, you'll often need to authenticate the client.
+// For example, imagine you have an authentication plug, `MyAuth`,
+// which authenticates the session and assigns a `:current_user`.
+// If the current user exists you can assign the user's token in
+// the connection for use in the layout.
+//
+// In your "lib/web/router.ex":
+//
+// pipeline :browser do
+// ...
+// plug MyAuth
+// plug :put_user_token
+// end
+//
+// defp put_user_token(conn, _) do
+// if current_user = conn.assigns[:current_user] do
+// token = Phoenix.Token.sign(conn, "user socket", current_user.id)
+// assign(conn, :user_token, token)
+// else
+// conn
+// end
+// end
+//
+// Now you need to pass this token to JavaScript. You can do so
+// inside a script tag in "lib/web/templates/layout/app.html.eex":
+//
+// <script>window.userToken = "<%= assigns[:user_token] %>";</script>
+//
+// You will need to verify the user token in the "connect/3" function
+// in "lib/web/channels/user_socket.ex":
+//
+// def connect(%{"token" => token}, socket, _connect_info) do
+// # max_age: 1209600 is equivalent to two weeks in seconds
+// case Phoenix.Token.verify(socket, "user socket", token, max_age: 1209600) do
+// {:ok, user_id} ->
+// {:ok, assign(socket, :user, user_id)}
+// {:error, reason} ->
+// :error
+// end
+// end
+//
+// Finally, connect to the socket:
+socket.connect()
+
+// Now that you are connected, you can join channels with a topic:
+let channel = socket.channel("topic:subtopic", {})
+channel.join()
+ .receive("ok", resp => { console.log("Joined successfully", resp) })
+ .receive("error", resp => { console.log("Unable to join", resp) })
+
+export default socket