41 lines
1.4 kB
1
defmodule BlogWeb.UserSocket do
2
use Phoenix.Socket
3
4
# A Socket handler
5
#
6
# It's possible to control the websocket connection and
7
# assign values that can be accessed by your channel topics.
8
9
## Channels
10
channel "fireworks", BlogWeb.FireworkChannel
11
12
# Socket params are passed from the client and can
13
# be used to verify and authenticate a user. After
14
# verification, you can put default assigns into
15
# the socket that will be set for all channels, ie
16
#
17
# {:ok, assign(socket, :user_id, verified_user_id)}
18
#
19
# To deny connection, return `:error` or `{:error, term}`. To control the
20
# response the client receives in that case, use `{:error, :reason, response}`
21
#
22
# See `Phoenix.Token` documentation for examples in
23
# performing token verification on connect.
24
@impl true
25
def connect(_params, socket, _connect_info) do
26
{:ok, socket}
27
end
28
29
# Socket id's are topics that allow you to identify all sockets for a given user:
30
#
31
# def id(socket), do: "user_socket:#{socket.assigns.user_id}"
32
#
33
# Would allow you to broadcast a "disconnect" event and terminate
34
# all active sockets and channels for a given user:
35
#
36
# Elixir.BlogWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{})
37
#
38
# Returning `nil` makes this socket anonymous.
39
@impl true
40
def id(_socket), do: nil
41
end
42