Skip to content

Commit

Permalink
Add websocket for subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Abshier committed Oct 4, 2021
1 parent 12bf5ce commit 7a5cca8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/gql_preferences/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule UserPreferences.Application do
@moduledoc false

use Application
import Supervisor.Spec
alias ActivityServer

@impl true
Expand All @@ -19,7 +20,8 @@ defmodule UserPreferences.Application do
UserPreferencesWeb.Endpoint,
# Start a worker by calling: UserPreferences.Worker.start_link(arg)
# {UserPreferences.Worker, arg}
ActivityServer
ActivityServer,
{Absinthe.Subscription, UserPreferencesWeb.Endpoint}
]

# See https://hexdocs.pm/elixir/Supervisor.html
Expand Down
1 change: 1 addition & 0 deletions lib/gql_preferences_web/endpoint.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule UserPreferencesWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :gql_preferences
use Absinthe.Phoenix.Endpoint

# The session will be stored in the cookie and signed,
# this means its contents can be read but not tampered with.
Expand Down
4 changes: 2 additions & 2 deletions lib/gql_preferences_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ defmodule UserPreferencesWeb.Router do
scope "/" do
pipe_through :api

forward "/api", Absinthe.Plug,
schema: UserPreferencesWeb.Schema
forward "/api", Absinthe.Plug, schema: UserPreferencesWeb.Schema

forward "/graphiql", Absinthe.Plug.GraphiQL,
schema: UserPreferencesWeb.Schema,
socket: UserPreferencesWeb.UserSocket,
interface: :simple
end

Expand Down
25 changes: 25 additions & 0 deletions lib/gql_preferences_web/user_socket.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule UserPreferencesWeb.UserSocket do
use Phoenix.Socket

use Absinthe.Phoenix.Socket,
schema: UserPreferencesWeb.Schema

def connect(params, socket) do
current_user = current_user(params)

socket =
Absinthe.Phoenix.Socket.put_options(socket,
context: %{
current_user: current_user
}
)

{:ok, socket}
end

defp current_user(%{"user_id" => id}) do
MyApp.Repo.get(User, id)
end

def id(_socket), do: nil
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ defmodule UserPreferences.MixProject do
[
{:absinthe, "~> 1.6"},
{:absinthe_plug, "~> 1.5"},
{:absinthe_phoenix, "~> 2.0"},
{:absinthe_relay, "~> 1.5"},
{:phoenix, "~> 1.6.0"},
{:phoenix_ecto, "~> 4.4"},
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
%{
"absinthe": {:hex, :absinthe, "1.6.6", "d4b3d87c868264edf47fbf9c152155f31e8d26c370607f5fe92f6e106d190b74", [:mix], [{:dataloader, "~> 1.0.0", [hex: :dataloader, repo: "hexpm", optional: true]}, {:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0 or ~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a03e18478b19bdf81ed1eef9b0853edf4496a080c2048ed17993dc945a90bedc"},
"absinthe_phoenix": {:hex, :absinthe_phoenix, "2.0.2", "e607b438db900049b9b3760f8ecd0591017a46122fffed7057bf6989020992b5", [:mix], [{:absinthe, "~> 1.5", [hex: :absinthe, repo: "hexpm", optional: false]}, {:absinthe_plug, "~> 1.5", [hex: :absinthe_plug, repo: "hexpm", optional: false]}, {:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.5", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.13 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}], "hexpm", "d36918925c380dc7d2ed7d039c9a3b4182ec36723f7417a68745ade5aab22f8d"},
"absinthe_plug": {:hex, :absinthe_plug, "1.5.8", "38d230641ba9dca8f72f1fed2dfc8abd53b3907d1996363da32434ab6ee5d6ab", [:mix], [{:absinthe, "~> 1.5", [hex: :absinthe, repo: "hexpm", optional: false]}, {:plug, "~> 1.4", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "bbb04176647b735828861e7b2705465e53e2cf54ccf5a73ddd1ebd855f996e5a"},
"absinthe_relay": {:hex, :absinthe_relay, "1.5.1", "adf298e77cf83d52bae1d7dc1579146bf9b893fcaa7b556d62e81a8c6f997514", [:mix], [{:absinthe, "~> 1.5.0 or ~> 1.6.0", [hex: :absinthe, repo: "hexpm", optional: false]}, {:ecto, "~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm", "1fd2a3559f8472c5bac5778c8b87ae5a5d7f89b594eba26b684ce1d0345a910a"},
"castore": {:hex, :castore, "0.1.12", "b5755d7668668a74c0e3c4c68df91da927e063a5cade17d693eff04e6ab64805", [:mix], [], "hexpm", "981c79528f88ec4ffd627214ad4cdd25052dc56c002996c603011ae37ec1b4b0"},
Expand Down

0 comments on commit 7a5cca8

Please sign in to comment.