Is it possible to reuse a widget that uses an output port?

I have a pretty self contained widget that uses an output port to send data to the backend via a websocket.

The output port signature is:

port sendMessage : Decode.Value -> Cmd msg

Is it possible for me to reuse this widget in a bigger Elm application? If so how? It doesn’t work transparently, and I couldn’t find a way to “subscribe” to the output port of the widget so that I can at least receive the data it’s trying to send and pipe it to the backend using a port from the main application.

Thanks for your help.

If sendMessage or Your.Widget.sendMessage is used anywhere in your app, then when you do, in JS,

const app = Elm.YourMain.init();


that app.ports.sendMessage will be available. If you’re aren’t using sendMessaage, then it will be undefined in JS.

My ports aren’t defined in Main.elm, they are defined in the file that defines my widget (let’s call it Widget.elm). I’m perfectly willing to define ports also in Main.elm, but then how do I get access to the data that Widget outputs so that I can output it again in Main?

Ok, actually that works transparently and the problem was in my update function (stupid typo that led to the model not being updated). Since it was the first time I was trying to embed a widget that used ports, I thought the problem came from this.
Morality: when trying to use a new technique, we always think what doesn’t work comes from a problem related to that new technique, but no, it’s still way more probable that it comes from something very simple and stupid.
I think I’ll keep that thread here, in case someone is wondering if it’s possible to embed a widget that uses ports in a bigger application. Yes, it’s actually possible and you don’t have to define ports in the “Main.elm”, you can use the ports defined in the widget.


If you want, I don’t know how but you can mark a particular response as the solution to your question. If you think that applies.

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