NodeJS desktop apps and tracking those elusive offline errors

| 3 min. (517 words)

“Any app that can be built in NodeJs, will eventually be built in NodeJS.”

– Me, borrowed from Jeff Atwood’s law

By now you’ve probably used NodeJS to build a little “Hello World” web server, just to see what the fuss is about. You may have even built a cool little web app that stores your to-do list. And possibly even deployed a big app into production running Node. But what about desktop applications?

We recently had a support request come through asking if the Raygun4Node provider had offline support for point-of-sales terminals that weren’t always connected to the internet. I was intrigued – I had never really considered building a ‘desktop application’ in Node, but technically it could be done, right? Turns out there’s a few people doing it with awesome results.

For the purposes of this post, I’ll be focusing on Electron which is based off of Atom, the nice editor from Github which power the likes of the Slack desktop app.

Getting start with Electron is pretty easy – everything is available on NPM and they even include an application generator to make getting started that bit easier. So away I went and created a cool ‘Hello World’ desktop application, dreaming of making it big in the ‘Hello World App Store’ and spending my days on the beach in the sun.

Obviously I want to be able to track errors that may happen in my awesome app, so Raygun4Node was installed, along with Raygun4JS, in a slightly odd dual Raygun setup for the very reason that Electron runs both frontend and backend code, sort of like a website, but also sort of not like a website. Cool eh?

After plugging my API keys in, errors started flowing through to Raygun no worries when I clicked the big ‘Test’ button. That was, right up until I turned off my wifi, then boom, no more errors! Sadface.

After a bit of thinking I decided it was time to add some offline support to the Raygun NodeJS provider to help with this in future. The updated package is available right now on NPM which means you can be up and running super quick.

Out of the box you get a disk based offline provider which is easy to setup and should work in most cases. Simply tell Raygun where to put files and how many to keep

var raygunClient = new raygun.Client().init(
    {
        apiKey: 'API-KEY',
        isOffline: false,
        offlineStorageOptions: {
            cachePath: 'raygunCache/',
            cacheLimit: 1000 // defaults to 100 errors if you don't set this
        }
    }
);

Once your application detects that it’s offline you need to let Raygun know – again, this is simple

raygunClient.offline();

And once you’re back online? You guessed it!

raygunClient.online();

Any cached errors will also be sent to Raygun at this point.

If disk based caching doesn’t quite work for you, Raygun4Node allows you to pass in your own custom offline provider. You can read more about getting this setup in the documentation. Offline support isn’t just limited to desktop applications either, you should be able to use it anywhere that NodeJS runs!