MQTT module for gjs?

Is there an MQTT client module available for use with gjs?

I’ve googled and searched this group but found nothing relevant, so assuming the answer is no, a supplementary question:

Is it possible for a gjs app to somehow use modules normally distributed through npm? There’s an npm client module there which I’ve used previously in a nodejs application. Can I use it in a gjs application?

Thanks,
chris

Is it possible for a gjs app to somehow use modules normally distributed through npm?

Yes with a bit of effort depending on what you are trying to use.

If it’s a pure ES module and doesn’t depend on anything extraneous to gjs - you can import it as is from node_modules.

If it depends on globals that gjs doesn’t support, you will need to polyfill them. For example setTimeout, atob, websocket, fetch … I did some (minimal) work there in GitHub - sonnyp/troll: Makes GNOME JavaScript cozy. - check older revisions for things that are now included in gjs if you need to support an older gjs version but I recommend upgrading to 1.72. For now, you can simply use org.gnome.Platform from flathub beta with

flatpak remote-add flathub-beta https://flathub.org/beta-repo/flathub-beta.flatpakrepo
flatpak install flathub-beta org.gnome.Platform//42beta
flatpak run --command="gjs" --filesystem="host" org.gnome.Platform//42beta -m myfile.js

For the other cases - you will need to “compile” the modules

  • It use commonjs (require / module.exports)
  • It relies on Node.js own/internal modules
  • It imports json
  • It has non relative imports

I use rollup for that, you can find an examples here and here - install rollup and the plugins and run npx rollup -c rollup.config.js

With polyfills and rollup, I’ve managed to get xmpp.js working in gjs, if mqtt client has a WebSocket transport it should be doable as well.

If mqtt exclusively relies on the Node.js net module, you will need to write a compat layer with Gio.

1 Like

Thanks for all that info Sonny - really helpful.

It’s great to know that it’s possible.

It doesn’t look like it would be a week end job though so it’s good I don’t need it immediately. Working with gjs for a couple of weeks has made me start thinking about converting another already existing nodejs app I finished recently.

chris

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