Pixie.Extension behaviour

Used to implement Bayeux extensions, which can be used to filter or change incoming messages and the responses sent back to the client.

For example:

defmodule AuthenticationExtension do
  use Pixie.Extension

  def handle %Event{message: %{ext: %{username: u, password: p}=message}}=event do
    case User.authenticate username, password do
      :ok ->
        %{event | message: %{message | ext: nil}}
      :error ->
        %{event | response: %{response | error: "Authentication Failed"}}
    end
  end

  def handle %Event{}=event do
    %{event | response: %{response | error: "Authentication Failed"}}
  end

Note that you must always provide a "catch all" function head that matches all other events and returns them - otherwise the runtime will start raising exceptions and generally make you feel sad.

You can dynamically add your extension to the extension stack at runtime using YourExtension.register and YourExtension.unregister

Source

Callbacks

handle/1

Specs:

Can be used to modify the Pixie.Event struct that is passed in.

The incoming message is provided in the message property, whereas the response being sent back to the sending client is stored in the response property.

Depending on the kind of message you are likely to also receive the PID of the Pixie.Client in the client property also.

Note that if you want to stop the message from being delivered then set it to nil, likewise if you want to stop a response being sent to the client. You must always return a Pixie.Event struct from handle/1.

Source