Meteor-Elm-Example

(I have posted this on the elm and on the meteor forums)

This is my attempt to use elm as the view layer for a meteor based project. I tried to keep this example as simple as possible. I want to mention that:

  • The example is a mega-counter example :wink: connected to a db (mongodb) on the server, so the data is persistent.

  • I have not used a meteor-build-plugin to compile elm, because it has not worked out for me. Instead I’m using the elm make command in combination with inotifywait to compile elm to js and then include the output via a meteor package to stop meteor from compiling the elm-output again.

  • Meteor owns the state of the published collections, elm is only responsible for displaying the data. (from the elm guide: state should be owned by Elm or by JavaScript, never both)

  • If you want to run the example on your locale machine, you have to install meteor and elm, clone the repo and then run meteor run watchelm and in another terminal meteor run.

  • The data is updated as you click on the numbers, so everyone sees the same counters changing live. You can open another browser window to watch the changes you are making.

live demo: https://meteor-elm-example.herokuapp.com
github repo: GitHub - ni-ko-o-kin/meteor-elm-example

If this has sparked you interest, let me know if I should post another more comprehensive example with authentication, validation, user permissions, rate limiting aso. Maybe the realworld example.

Feedback is welcome!

6 Likes

This is super cool!

I was looking at elm just the other week, wondering how you might integrate it with meteor.

2 Likes

That’s very good. Anything new about building mobile app?

1 Like

I haven’t used elm for mobile apps yet.

This is great to see, thank you for sharing. Have been interested in the potential for using Elm as a view layer Meteor and would be really excited to see a clean integration at some point. Since you asked, I would love to see a more comprehensive example and walk-through to connect them.
Cheers

A clean integration would be awesome. But would be different on the server, as elm is design to work in the browser. I have created an example on how you could use elm on node as well: https://github.com/ni-ko-o-kin/elm-node

Interesting!
I had not even considered the possibility of full-stack Elm in Meteor, but I love the sound of it!
I imagine the client piece will be a lot easier to start with though. :sweat_smile:

Could you post more detailed instruction for getting started after cloning the repo?
If I just run meteor run I get
Error: ENOENT: no such file or directory, open '/Users/gabrielk/projects/spotify/meteor-elm-example/packages/elm/elm-main.js'

I’m guessing I need to do something before running meteor? How/What?

You need to compile elm to js first: meteor npm run watchelm. I updated the readme.

Thanks so much for the quick reply!
Is it also required to chmod +x the *.sh files?

So I ran meteor install and then meteor npm run watchelm and got error:

> meteor-elm-example@ watchelm /Users/gabrielk/projects/spotify/meteor-elm-example
> sh ./private/watchelm.sh

Success!

    Main ───> ../packages/elm/elm-main.js

./private/watchelm.sh: line 5: inotifywait: command not found

Do I need to first install something?
edit: ah, inotifywait is linux specific… I’m on Mac OS. But perhaps fswatch can be used on Mac OS instead…

./private/watchelm.sh: line 5: inotifywait: command not found:
→ you need to install inotify-tools

But that command is specific for linux, you will need something like this:

Yes. I replaced inotifywait with fswatch (brew install fswatch) in the script and then it worked!
Thanks! :slight_smile:

:+1: I will update the example so it works on mac and windows as well soon. Do you mind testing the install instruction when I have them ready. I don’t have a mac.

1 Like

Btw I updated to elm 0.19.1 and seems to work fine. Also updated meteor ( meteor update --all-packages). Thought you could also do that and commit those changes.
I think some packages must be updated manually (such as mongo).

Did you update fswatch script?

No, not yet. This has to wait.

But I improved the watchelm.sh script and updated meteor and elm.