On the Javascript side, the Elm `app` is like an event emitter where you listen for updates and send it data.
var app = Elm.Main.embed(...)
app.ports.gameState.subscribe((data) => ...)
app.ports.tileClick.send([x, y])
On the Elm side, you subscribe to incoming ports and map them into Msgs. And you emit to ports with Cmds from your update function. -- Ports.elm
port gameState : Json.Encode.Value -> Cmd msg // Outgoing
port tileClicked : ((Int, Int) -> msg) -> Sub msg // Incoming
-- Main.elm
update msg model =
case msg of
Tick ->
-- Define `encode` to Json.Encode the game state
(model, Ports.gameState (GameState.encode data))
TileClicked (x, y) ->
-- Handle tile clicks sent in from Javascript
subscriptions model =
Ports.tileClicked TileClicked