This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
Elm ports' wrapper for uncomplicated request-response-style communication
version 1.0.0
license BSD3
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 1.0.0
Committed At 2017-10-14 13:58:07 UTC
elm-lang/core 5.1.1 <= v < 6.0.0 5.1.1

Modules

README

elm-porter

Port message manager that emulate a request-response style communication, a'la Http.send Response request.

How it works

port module Main exposing (..)

import Porter
import Html exposing (text)
import Json.Encode as Encode
import Json.Decode as Decode

-- Configure Porter

port outgoing : Encode.Value -> Cmd msg
port incoming : (Decode.Value -> msg) -> Sub msg

porterConfig =
    { outgoingPort = outgoing
    , incomingPort = incoming
    -- Porter works with a single Request and Response data types. They can both be anything, as long as you supply decoders :)
    , encodeRequest = Encode.string
    , decodeResponse = Decode.string
    }

-- Application model

init =
  ( { porter = Porter.init
    , response = ""
    }
  -- Send a request through porter, specifying the response handler directly
  , Porter.send Receive "Reverse me!"
  )

-- Message includes Porter's message

type Msg
  = PorterMsg Porter.Msg
  | Receive String

-- Update porter accordingly

update msg model =
  case msg of
    PorterMsg porterMsg ->
      { model | porter = Porter.update porterConfig porterMsg model.porter }

    Receive response ->
      { model | response = response }

-- Set up porter's subscriptions

subscriptions model =
  Porter.subscriptions porterConfig

-- Any view you like

view model =
  text (toString model)

-- 
main =
  program
    { init = init
    , update = update
    , subscriptions = subscriptions
    , view = view
    }

Handling messages in JS

Outgoing messages into JavaScript as JSON, with an id that Porter generates and uses to match up with response handlers.

ports.outgoing.subscribe(msgWithId => {
  const id = msgWithId.id
  const request = msgWithId.msg
  const response = request.reverse()
  // Simply include the same `id` and the response under the `msg` key.
  ports.incoming.send({
    id: id,
    msg: resonse
  })
})