While trying myself to implement a port as exposedhere, I couldn’t find a way to know why port functions are not defined; actually, the elm linter told me that (kudos for the linter tho!!).
Therefore, why is that any common function must have a declaration whereas port function don’t? How does it work then? Does it just work with JSON-encoded values? Where can I get more info. on how Cmd msg?
A command (Cmd) is the way Elm allows our init or update function to return a “side-effect”.
A side-effect can be sending like sending an HTTP request to an API, or communicating with JavaScript via ports. It’s for doing things other than returning pure data.
For ports to work, let’s make sure we do these three things:
Use port module instead of module at the top of the file.
Declare a port like this: port outgoing : String -> Cmd msg
Handle it in your JavaScript code:
app.ports.outgoing.subscribe( function (message) {
console.log(message)
}
If you have more questions on ports, come join the official Elm slack!