# 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.

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

### Examples

**Creating a Web Worker**

Instantiate a Web Worker from your main application. The Coralogix RUM SDK automatically detects the worker and starts capturing unhandled errors and supported events, no additional instrumentation needed on the main thread.

```javascript
// App code – creating a worker. The SDK will automatically capture errors from it.

const worker = new Worker('my-worker.js');
```

**Logging from inside a Web Worker**

Log an informational message directly from within the Web Worker. This ensures that background logs are recorded and appear alongside main-thread events in Coralogix.

```javascript
// Log a basic info log
worker.CoralogixRum.log(CoralogixLogSeverity.Info, 'Test log from worker');
```

**Capturing errors with context**

Catch and manually report errors using `captureError`. Add contextual metadata (e.g. the worker’s role or custom labels) to enrich your observability and streamline debugging.

```javascript
// Manually capture errors
try {
  // Some code that might throw an error
} catch (error) {
  CoralogixRum.captureError(error, { worker: 'analytics-worker' }, { label1: 'value1' });
}
```

**Logging incoming messages in the Worker**

Track and log messages received by the worker via `postMessage`. This helps monitor interactions between the main thread and the worker, improving traceability of distributed logic.

```javascript
// Log messages received in the worker
self.onmessage = (message) => {
  worker.CoralogixRum.log(CoralogixLogSeverity.Info, message);
};
```
