I think the problem with WebRTC is the non-existence of universal signaling protocol.
Disregarding how the signaling server code would work, the API would probably have to look something like:
type ConnectionStatus
= Disconnected
| ConnectedNotReady
| ConnectedReady
listenOnly : String -> (ConnectionStatus -> msg) -> (Json.Decode.Value -> msg) -> Sub msg
listenOnly connectionName setConnectionStatusMsg getDataMsg = ...
sendOnly : String -> (ConnectionStatus -> msg) -> Sub msg
sendOnly connectionName setConnectionStatusMsg = ...
listenAndSend : String -> (ConnectionStatus -> msg) -> (Json.Decode.Value -> msg) -> Sub msg
listenAndSend connectionName setConnectionStatus getDataMsg = ...
send : String -> Json.Decode.Value -> Cmd msg
send connectionName value = ...
I am creating an Elm app with three WebRTC peers (library, controller, output) and the graph like this:
- library -> controller
- library -> output
- controller -> output
So that would translate into (controller):
type Peer
= Library
| Output
type Msg
= SetPeerStatus Peer WebRTC.ConnectionStatus
| SendDataToOutput -- sent periodically or onClick of a button
| GetDataFromLibrary Json.Decode.Value
type alias DataFromLibrary =
Bool
type alias DataForOutput =
Int
type alias Model =
{ webRtcStatus : List (Peer, WebRTC.ConnectionStatus)
, dataFromLibrary : Maybe DataFromLibrary
, dataForOutput : DataForOutput
}
update msg model =
case msg of
SetPeerStatus peer status ->
( { model | webRtcStatus =
List.Extra.updateIf
(\(oldPeer, _) -> oldPeer == peer)
(\_ -> (peer, status))
model.webRtcStatus
}
, Cmd.none
)
SendDataToOutput ->
( model
, WebRTC.send "output" model.data
)
GetDataFromLibrary json ->
( model -- do something with JSON, eg. Json.Decode.decodeValue libraryDataDecoder. This part is big so I did not include it. But it can be done :)
, Cmd.none
)
subscriptions model =
Sub.batch
[ WebRTC.listenOnly "library" (SetPeerStatus Library) GetDataFromLibrary
, WebRTC.sendOnly "output" (SetPeerStatus Output)
]
I don't know how/whether to handle reconnections, error values (as in WebSocket.LowLevel), ...
Does this kind of API make sense? Gabor, did you make any progress with this effect manager?