Phoenix.Channel

A channel declares which topic should be joined, registers event handlers and has various callbacks for possible lifecycle events.

Definition

type alias Channel msg = PhoenixChannel msg

Representation of a Phoenix Channel

Helpers

init : String -> Channel msg

Initialize a channel to a given topic.

init "room:lobby"
withPayload : Value -> Channel msg -> Channel msg

Attach a payload to the join message. You can use this to submit e.g. a user id or authentication infos. This will be the second argument in your join/3 callback on the server.

payload =
    Json.Encode.object
      [
        ( "user_id"
        , Json.Encode.string "123"
        )
      ]

init "room:lobby"
    |> withPayload payload
on : String -> (Value -> msg) -> Channel msg -> Channel msg

Register an event handler for a event.

type Msg = NewMsg Value | ...

init "roomy:lobby"
    |> on "new_msg" NewMsg
onJoin : (Value -> msg) -> Channel msg -> Channel msg

Set a callback which will be called after you sucessfully joined the channel. It will also be called after you rejoined the channel after a disconnect unless you specified an onRejoin handler.

type Msg =
    IsOnline Json.Encode.Value | ...

init "room:lobby"
    |> onJoin IsOnline
onJoinError : (Value -> msg) -> Channel msg -> Channel msg

Set a callback which will be called if the server declined your request to join the channel.

type Msg =
    CouldNotJoin Json.Encode.Value | ...

init "room:lobby"
    |> onJoinError CouldNotJoin

Note: If a channel declined a request to join a topic the effect manager won_t try again.

onError : msg -> Channel msg -> Channel msg

Set a callback which will be called if the channel process on the server crashed. The effect manager will automatically rejoin the channel after a crash.

type Msg =
     ChannelCrashed | ...

init "room:lobby"
    |> onError ChannelCrashed
onDisconnect : msg -> Channel msg -> Channel msg

Set a callback which will be called if the socket connection got interrupted. Useful to switch the online status to offline.

type Msg =
    IsOffline | ...

init "room:lobby"
    |> onDisconnect IsOffline

Note: The effect manager will automatically try to reconnect to the server and to rejoin the channel. See onRejoin for details.

onRejoin : (Value -> msg) -> Channel msg -> Channel msg

Set a callback which will be called after you sucessfully rejoined the channel after a disconnect. Useful if you want to catch up missed messages.

type Msg =
    IsOnline Json.Encode.Value | ...

init "room:lobby"
    |> onRejoin IsOnline
onLeave : (Value -> msg) -> Channel msg -> Channel msg

Set a callback which will be called after you sucessfully left a channel.

type Msg =
    LeftLobby Json.Encode.Value | ...

init "room:lobby"
    |> onLeave LeftLobby
onLeaveError : (Value -> msg) -> Channel msg -> Channel msg

Set a callback which will be called if the server declined your request to left a channel. (It seems that Phoenix v1.2 doesn_t send this)

Print all status changes.

map : (a -> b) -> Channel a -> Channel b

Composes each callback with the function a -> b.