Skip to main content

Web worker support

Web Worker support enables the SDK to capture events from within Web Workers, including unhandled exceptions, custom log messages, and other SDK-provided features, ensuring complete visibility across all execution contexts.

Overview

Enabling Web Worker support ensures complete visibility into background execution contexts. This allows you to monitor, debug, and optimize multi-threaded workflows just as effectively as your main application logic.

Gain full observability

  • Capture unhandled errors and logs from Web Workers to monitor all execution contexts.

  • Expose hard-to-detect issues like silent failures in background threads.

Simplify debugging

  • Trace issues across main-thread and worker contexts with unified logs.

  • Identify race conditions and background logic failures with contextual event data.

Extend SDK capabilities

  • Enable full use of SDK features, such as error tracking, custom logging, and more, within workers.

  • Unify your observability pipeline across both synchronous and asynchronous code paths.

Support scalable architectures

  • Instrument background tasks in performance-optimized apps using workers.

  • Ensure reliable monitoring in modern, multi-threaded front-end architectures.

Enable Web Worker support

By default, Web Worker support is disabled. Enable this mode as follows.

CoralogixRum.init({
workerSupport: true,
});

What is captured automatically

Once workerSupport is on, the SDK wraps Worker, so any worker you create with new Worker(...) reports its unhandled runtime errors and message deserialization failures on its own. Nothing needs to be added inside the worker script.

// App code - creating a worker. Its errors are captured automatically.
const worker = new Worker('my-worker.js');

Logging about a worker

The SDK attaches a CoralogixRum handle to the worker object it returns, so you can send your own logs and errors for that worker from the code that created it.

// App code, on the main thread
const worker = new Worker('my-worker.js');

worker.CoralogixRum.log(CoralogixLogSeverity.Info, 'worker started');

worker.onmessage = (message) => {
worker.CoralogixRum.log(CoralogixLogSeverity.Info, message.data);
};

try {
// work that might throw
} catch (error) {
worker.CoralogixRum.captureError(error, { worker: 'analytics-worker' }, { label1: 'value1' });
}
Note

worker.CoralogixRum exists on the worker object in the page, not inside the worker script. There is no Coralogix global in worker scope, so calls like worker.CoralogixRum.log(...) or CoralogixRum.log(...) placed in the worker file itself will fail. To report something the worker computed, post it back to the page and log it there.

Last updated on