phollyer/elm-phoenix-websocket/1.0.2

NOTE: the latest version is 4.0.0

Presence

This module is for working with Phoenix Presence.

Before you can receive presence information, you first need to connect to a socket and join a channel. Once this is done, you can then subscribe to start receiving Presence data as your users come and go.

If you are new to Phoenix Presence, you will need to setup your backend channel as described here. Currently, only the metas key is supported.

Receiving Messages

subscriptions : (EventIn -> msg) -> PortIn msg -> Sub msg

Subscribe to receive incoming presence events.

import Presence
import Ports.Phoenix as Phx


type Msg
  = PresenceMsg Presence.EventIn
  | ...




subscriptions : Model -> Sub Msg
subscriptions _ =
    Phx.presenceReceiver
      |> Presence.subscriptions
        PresenceMsg
type EventIn
= Join Topic Presence
| Leave Topic Presence
| State Topic PresenceState
| Diff Topic PresenceDiff
| InvalidEvent Topic String

All of the presence events that can come from the channel.

If you are using more than one channel, then you can check Topic to determine which channel the EventIn relates to. If you are only using a single channel, you can ignore Topic.

InvalidEvent means that an event has been received from the accompanying JS that cannot be handled. This should not happen, if it does, please raise an issue.

type alias Topic =
String

A type alias representing the channel topic. Use this to identify the channel an EventIn relates to.

If you are only using one channel, you can ignore this.

type alias Presence =
{ id : String
, metas : List Value
}

A type alias representing a Presence.

type alias PresenceState =
List Presence

A type alias representing a list of all Presences stored on the channel.

type alias PresenceDiff =
{ joins : List Presence
, leaves : List Presence
}

A type alias representing the joins and leaves on the channel as they happen.

type alias PortIn msg =
(Package -> msg) -> Sub msg

A type alias representing the port function required to receive the EventIn from the channel.

You could write this yourself, if you do, it needs to be named presenceReceiver, although you may find it simpler to just add this port module to your src - it includes all the necessary port functions.

type alias Package =
{ topic : String
, event : String
, payload : Value
}

A type alias representing the data received from a channel. You will not use this directly.

Decoders

decodeMetas : Decoder a -> List Value -> List a

Decode the metas for a Presence.

Only you will know what meta information you are storing, so you will need to provide your own decoder.