View Source Absinthe.Phoenix.Socket (absinthe_phoenix v2.0.3)

Absinthe.Phoenix.Socket is used as a module for setting up a control channel for handling GraphQL subscriptions.

Examples

defmodule MyApp.Web.UserSocket do
  use Phoenix.Socket
  use Absinthe.Phoenix.Socket,
    schema: MyApp.Web.Schema

  transport :websocket, Phoenix.Transports.WebSocket

  def connect(params, socket) do
    socket = Absinthe.Phoenix.Socket.put_options(socket, [
      context: %{current_user: find_current_user(params)}
    ])
    {:ok, socket}
  end

  def id(_socket), do: nil
end

Phoenix 1.2

If you're on Phoenix 1.2 see put_schema/2.

Garbage Collection

In some workloads, the Channel Process responsible for handling subscriptions may accumulate some gargage, that is not being collected by the Erlang VM. A workaround for this is to instruct the process to periodically tell the VM to collect its garbage. This can be done by setting the gc_interval.

  use Absinthe.Phoenix.Socket,
    schema: MyApp.Web.Schema,
    gc_interval: 10_000

Summary

Functions

Configure Absinthe options for a socket.

Configure the schema for a socket.

Functions

Link to this function

put_options(socket, opts)

View Source
@spec put_options(Phoenix.Socket.t(), Absinthe.run_opts()) :: Phoenix.Socket.t()

Configure Absinthe options for a socket.

Examples

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
Link to this function

put_schema(socket, schema)

View Source

Configure the schema for a socket.

Only use this if you are not yet on Phoenix 1.3. If you're on Phoenix 1.3, read the moduledocs.