Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bendyworks/elm-action-cable
Client-side Elm library for ActionCable, part of the Ruby on Rails suite
https://github.com/bendyworks/elm-action-cable
actioncable elm elm-lang rails
Last synced: 2 months ago
JSON representation
Client-side Elm library for ActionCable, part of the Ruby on Rails suite
- Host: GitHub
- URL: https://github.com/bendyworks/elm-action-cable
- Owner: bendyworks
- License: other
- Created: 2017-03-31T22:21:42.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-04-06T21:36:00.000Z (over 7 years ago)
- Last Synced: 2024-10-15T16:20:47.803Z (2 months ago)
- Topics: actioncable, elm, elm-lang, rails
- Language: Elm
- Homepage: http://package.elm-lang.org/packages/bendyworks/elm-action-cable/latest
- Size: 67.4 KB
- Stars: 6
- Watchers: 10
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# ActionCable in Elm
This package is an Elm client library for [ActionCable][ActionCable guide], which comes bundled with Ruby on Rails.
## Basic Usage
```elm
import ActionCable
import ActionCable.Msg as ACMsg
import ActionCable.Identifier as IDinitModel : (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 Stringupdate : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
SubscribeTo roomName ->
subscribeTo roomName modelUnsubscribeFrom roomName ->
unsubscribeFrom roomName modelHandleData 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 modelCableMsg 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
* [Brad Grzesiak](https://twitter.com/listrophy), [Bendyworks](http://bendyworks.com)
[ActionCable guide]: http://guides.rubyonrails.org/action_cable_overview.html "ActionCable guide"