Subscribes to multiple events

Hi all,

I already managed to use port to plug on websocket or perform API call. But only one at a time.

For a new project I need to have more than one port and I couldn’t get out of it.

Here is some pseudo code of what I want to achieve:

JS part:

const app = Elm.Main.init({ node: document.getElementById("elm") });

// ...

app.ports.comingFromJsA.send("Message from A");

// ...

app.ports.comingFromJsB.send("Response from B");

Elm part:

---- PORTS ----

port comingFromJsA : (String -> msg) -> Sub msg

port comingFromJsB : (String -> msg) -> Sub msg

---- SUBS ----

subscriptions : Model -> Sub Msg
subscriptions _ =
    comingFromJsA MesgA
    -- how to dispatch to "comingFromJsB MesgB" ?

---- UPDATE ----

type Msg
    = MesgA String
    | MesgB String

I don’t get how subscriptions are supposed to make the link between JS and ports. Put another way, how do I trigger MeagA or MesgB depending on what has been sent by JS ?

All examples I have found are trivial and only deal with one type of message.


You want to subscribe to both A and B like this?

1 Like

you may find it easier in the long run to have just 1 port; to tag each message as it traverses the port; and have case/switch statement on each end to handle the payload

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.