By
Jason Skowronski

New and improved JavaScript notifier SDK - rollbar.js 2.0

We are excited to announce a major update to our JavaScript notifier SDK in version 2.0. This new version adds support for isomorphic or universal applications, which can run on both the client and the server. It standardizes the configuration and logging across environments, and makes it consistent with notifiers for other languages. Here are the big changes:

Single library for both client and server side

The JavaScript ecosystem has evolved a tremendous amount over the past several years. During that time, server side JavaScript usage has grown to the point where it is quite common to be using JavaScript throughout an application's stack. Applications now are often designed to run both in the client browser and on the server side using Node. These are commonly referred to as "isomorphic" or "universal" applications. The benefits of isomorphic applications include faster loading times, better support for legacy or mobile browsers, and easier search engine indexing.

With the line between client and server blurring, it is desirable to have a unified view of exceptions across both environments. To make this possible, we unified the separate notifier libraries so that exceptions are logged uniformly. One require to rule them all.

Consistent interface for configuration and logging across multiple environments

We now offer a consistent interface for configuration and logging for both client and server side. Furthermore, it is consistent with our notifier libraries for other languages. This will make it much easier for developers since they only need to use one pattern.

The new way to configure Rollbar is to create a configuration object. Regardless of where you’re running, the schema for the configuration object is exactly the same in both environments. You can choose to initialize Rollbar based on what’s most convenient in your environment. If you’re running within a browser environment, Rollbar can automatically read from the _rollbarConfig variable on the window when loading.

<script>
var _rollbarConfig = {
    accessToken: "POST_CLIENT_ITEM_ACCESS_TOKEN",
    captureUncaught: true,
    captureUnhandledRejections: true,
    payload: {
        environment: "production"
    }
};
// Load Rollbar snippet or library
...

// log a generic message and send to Rollbar
rollbar.log('Hello world!');
</script>

If you’re using a module loader or running in a server environment, you can pass this configuration block to the constructor.

var Rollbar = require('rollbar');
var rollbar = new Rollbar({
  accessToken: 'POST_SERVER_ITEM_ACCESS_TOKEN',
  handleUncaughtExceptions: true,
  handleUnhandledRejections: true
});

// log a generic message and send to Rollbar
rollbar.log('Hello world!');

Additionally, we have configuration examples for frameworks like Angular, Express, Hapi, and more. You can see these examples in the JavaScript SDK documentation.

Multiple Rollbar instances

Rollbar is no longer treated as a singleton, but can have multiple instances. The benefit of instances is that you can have different loggers with different configuration options in your application. This is helpful, for example, in pages with components tracked in separate Rollbar projects. For convenience and backwards compatibility, we still instantiate a window.Rollbar object.

Upgrading from the old node_rollbar library

The upgrade path from node_rollbar version 0.6.4 to version 2.0.0 of this library is not automatic, but it should be straightforward. While it is backwards compatible, we encourage you to update to the new syntax to take advantage of future functionality. The main changes are to library initialization and usage.

When you’re initializing the library, we now offer instances with a constructor instead of just a singleton as mentioned previously. Also, when using the library, methods like reportMessage/reportError/… have been deprecated in favor of log/warning/error/… These methods are more consistent with our notifier interface in other languages. They can accept a variety of parameters, including strings, errors, callbacks, and more.

Old:

var rollbar = require("rollbar");
rollbar.init("POST_SERVER_ITEM_ACCESS_TOKEN");
rollbar.reportMessage("Hello world!");

New:

var Rollbar = require("rollbar");
var rollbar = new Rollbar({
  accessToken: 'POST_SERVER_ITEM_ACCESS_TOKEN'
});
rollbar.log("Hello world!");

Additionally, you now enable logging of unhandled exceptions and rejections through the configuration object instead of making a separate method call. More details are available in the JavaScript SDK documentation.

To make the library backwards compatible and make migrating easier, we provided a variety of helper functions. For example, the old style was to always pass the access token as the first parameter. We permit this style for convenience when no other options are necessary, but for new code one should use an object as the only argument. Furthermore, the reportMessage/reportError/… methods are deprecated, but they are still in the code and just delegate to the log/warning/error/… methods. We encourage you to update to the newest logging methods.

If you have any issues or requests, we welcome you to file them in GitHub. Since the notifier is open source, we welcome your contributions and help improving the library.


If you haven’t already, sign up for a 14-day free trial of Rollbar and let us help you take control of annoying JavaScript errors.

Comments