// NOTE: The contents of this file will only be executed if
// you uncomment its entry in "web/static/js/app.js".

// To use Phoenix channels, the first step is to import Socket
// and connect at the socket path in "lib/my_app/endpoint.ex":
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 "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 "web/templates/layout/app.html.eex":
//
//     <script>window.userToken = "<%= assigns[:user_token] %>";</script>
//
// You will need to verify the user token in the "connect/2" function
// in "web/channels/user_socket.ex":
//
//     def connect(%{"token" => token}, socket) 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, pass the token on connect as below. Or remove it
// from connect if you don't care about authentication.

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

@@ -1974,28 +1974,459 @@
 el(%22
-topic:subtopic%22, %7B%7D)
+lobby%22, %7B%7D);%0Alet list    = $('#message-list');%0Alet message = $('#message');%0Alet name    = $('#name');%0A%0Amessage.on('keypress', event =%3E %7B%0A  if (event.keyCode == 13) %7B%0A    channel.push('new_message', %7B name: name.val(), message: message.val() %7D);%0A    message.val('');%0A  %7D%0A%7D);%0A%0Achannel.on('new_message', payload =%3E %7B%0A  list.append(%60%3Cb%3E$%7Bpayload.name %7C%7C 'Anonymous'%7D:%3C/b%3E $%7Bpayload.message%7D%3Cbr%3E%60);%0A  list.prop(%7BscrollTop: list.prop(%22scrollHeight%22)%7D);%0A%7D);%0A
 %0Acha

Establish lobby channel, basic message functions