Giter VIP home page Giter VIP logo

elm-action-cable's Introduction

ActionCable in Elm

This package is an Elm client library for ActionCable, which comes bundled with Ruby on Rails.

Basic Usage

import ActionCable
import ActionCable.Msg as ACMsg
import ActionCable.Identifier as ID


initModel : (Model, Cmd Msg)
initModel =
    { cable =
        ActionCable.initCable "ws://localhost:3000/cable/"
            |> ActionCable.onDidReceiveData (Just HandleData)
            |> ActionCable.withDebug True
    , errorToast = Nothing
    , ...
    } ! []


type Msg
    = CableMsg ACMsg.Msg
    | SubscribeTo String
    | UnsubscribeFrom String
    | HandleData ID.Identifier Json.Decode.Value
    | SendData String String


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        SubscribeTo roomName ->
          subscribeTo roomName model

        UnsubscribeFrom roomName ->
          unsubscribeFrom roomName model

        HandleData identifier value ->
            -- value is a Json.Decode.Value. You'll probably want to use
            -- Json.Decode.decodeValue
            { model | messages = toString value :: model.messages } ! []

        SendData roomName aStringToSend ->
          sendData roomName aStringToSend model

        CableMsg cableMsg ->
            -- important to forward on "accounting" messages to the underlying submodel
            ActionCable.update cableMsg model.cable
                |> (\( cable, cmd ) -> { model | cable = cable } ! [ cmd ])


subscribeTo : String -> Model -> (Model, Cmd Msg)
subscribeTo roomName model =
    case ActionCable.subscribeTo (channelId roomName) model.cable of
        Ok ( cable, cmd ) ->
            { model | cable = cable } ! [ Cmd.map CableMsg cmd ]

        Err err ->
            -- you're probably already subscribed to this channel
            { model | errorToast = Just <| ActionCable.errorToString err } ! []


unsubscribeFrom : String -> Model -> (Model, Cmd Msg)
unsubscribeFrom roomName model =
    case ActionCable.unsubscribeFrom (channelId roomName) model.cable of
        Ok ( cable, cmd ) ->
            { model | cable = cable } ! [ Cmd.map CableMsg cmd ]

        Err err ->
            -- you're probably already unsubscribed from this channel
            { model | errorToast = Just <| ActionCable.errorToString err } ! []


sendData : String -> String -> Model -> (Model, Cmd Msg)
sendData roomName aStringToSend model =
    let
        sendCmd =
            ActionCable.perform
                "some_channel_action_name"
                [ ( "jsonKey", Json.Encode.string aStringToSend ) ]
                (channelId roomName)
                model.cable
    in
        case sendCmd of
            Ok toSend ->
                model ! [ toSend ]

            Err err ->
                -- probably because you haven't subscribed to the channel yet
                { model | errorToast = Just <| ActionCable.errorToString err } ! []


channelId : String -> ID.Identifier
channelId roomName =
    ID.newIdentifier "ChatChannel" [ ( "room", roomName ) ]


-- subscriptions


subscriptions : Model -> Sub Msg
subscriptions model =
    ActionCable.listen CableMsg model.cable

License

elm-action-cable is released under the Apache v2 license, the details of which can be found in the LICENSE file.

Author

elm-action-cable's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.