View Source PS2.Socket (PlanetSide 2 API v0.3.3)

A Websockex client that connects to Planetside's Event Streaming Service (ESS).

After writing a PS2.SocketClient, you can start receiving and handling ESS events by spinning up a PS2.Socket with your desired event subscriptions. You should start this process in your supervision tree. For example:

defmodule MyApp.Application do
  use Application

  @impl Application
  def start(_type, _args) do
    subscriptions = [
            events: [PS2.player_login],
            worlds: [PS2.connery, PS2.miller, PS2.soltech],
            characters: ["all"]
        ]

    clients = [MyApp.EventHandler]

    ess_opts = [
      subscriptions: subscriptions,
      clients: clients,
      service_id: YOUR_SERVICE_ID,
      # you may also add a :name option. The name defaults to `PS2.Socket`, so if you want to run multiple sockets
      # for some reason, you can specify `name: :none` for no name to be registered.
    ]

    children = [
      # ...
      {PS2.Socket, ess_opts},
      # ...
    ]

    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

You can also include metadata in the ESS opts to be passed with every event. For example:

  ess_opts = [
    subscriptions: subscriptions,
    clients: clients,
    service_id: YOUR_SERVICE_ID,
    metadata: [hello: :websocket]
  ]

  # in your SocketClient:
  def handle_event({event_name, _payload}, metadata) do
    IO.inspect("Received #{event_name} with metadata #{inspect(metadata)}")
  end

Since your service ID should be kept a secret, if you're using version control (e.g. git), you should use Application.get_env(:your_app, :service_id), or use environment variables with System.get_env(:your_app, :service_id), in place of YOUR_SERVICE_ID. You can read more about configuring Elixir applications in Nicd's awesome blog post.

Link to this section Summary

Functions

Resubscribe to all events

Add a new subscription. Raises a KeyError if subscription is not formatted correctly.

Link to this section Functions

Link to this function

resubscribe(name \\ __MODULE__)

View Source

Resubscribe to all events

Link to this function

subscribe!(name \\ __MODULE__, subscription)

View Source

Add a new subscription. Raises a KeyError if subscription is not formatted correctly.

subscription should be a keyword list similar to the one passed in the options to PS2.Socket.