Phoenix

An Elm client for Phoenix Channels.

This package makes it easy to connect to Phoenix Channels, but in a more declarative manner than the Phoenix Socket Javascript library. Simply provide a Socket and a list of Channels you want to join and this library handles the tedious parts like opening a connection, joining channels, reconnecting after a network error and registering event handlers.

Connect with Phoenix

connect : Socket -> List (Channel msg) -> Sub msg

Declare a socket you want to connect to and the channels you want to join. The effect manager will open the socket connection, join the channels. See Phoenix.Socket and Phoenix.Channel for more configuration and behaviour details.

import Phoenix.Socket as Socket
import Phoenix.Channel as Channel

type Msg = NewMsg Value | ...

socket =
    Socket.init "ws://localhost:4000/socket/websocket"

channel =
    Channel.init "room:lobby"
        -- register a handler for messages
        -- with a "new_msg" event
        |> Channel.on "new_msg" NewMsg

subscriptions model =
    connect socket [channel]

Note: An empty channel list keeps the socket connection open.

Push messages

push : String -> Push msg -> Cmd msg

Pushes a Push message to a particular socket address. The address has to be the same as with which you initalized the Socket in the connect subscription.

payload =
    Json.Encode.object
      [ ( "msg"
        ,  Json.Encode.string "Hello Phoenix"
        )
      ]

message =
    Push.init "room:lobby" "new_msg"
        |> Push.withPayload payload

push "ws://localhost:4000/socket/websocket" message

Note: The message will be queued until you successfully joined a channel to the topic of the message.