user_socket.ex 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. defmodule WebWeb.UserSocket do
  2. use Phoenix.Socket
  3. ## Channels
  4. # channel "room:*", WebWeb.RoomChannel
  5. ## Transports
  6. transport :websocket, Phoenix.Transports.WebSocket
  7. # transport :longpoll, Phoenix.Transports.LongPoll
  8. # Socket params are passed from the client and can
  9. # be used to verify and authenticate a user. After
  10. # verification, you can put default assigns into
  11. # the socket that will be set for all channels, ie
  12. #
  13. # {:ok, assign(socket, :user_id, verified_user_id)}
  14. #
  15. # To deny connection, return `:error`.
  16. #
  17. # See `Phoenix.Token` documentation for examples in
  18. # performing token verification on connect.
  19. def connect(_params, socket) do
  20. {:ok, socket}
  21. end
  22. # Socket id's are topics that allow you to identify all sockets for a given user:
  23. #
  24. # def id(socket), do: "user_socket:#{socket.assigns.user_id}"
  25. #
  26. # Would allow you to broadcast a "disconnect" event and terminate
  27. # all active sockets and channels for a given user:
  28. #
  29. # WebWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{})
  30. #
  31. # Returning `nil` makes this socket anonymous.
  32. def id(_socket), do: nil
  33. end