SharedWorkerGlobalScope: connect event

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

The connect event is fired in shared workers at their SharedWorkerGlobalScope when a new client connects.

This event is not cancelable and does not bubble.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js
addEventListener("connect", (event) => { })

onconnect = (event) => { }

Event type

A MessageEvent. Inherits from Event.

Event MessageEvent

Event properties

This interface also inherits properties from its parent, Event.

MessageEvent.data Read only

The data sent by the message emitter.

MessageEvent.origin Read only

A string representing the origin of the message emitter.

MessageEvent.lastEventId Read only

A string representing a unique ID for the event.

MessageEvent.source Read only

A MessageEventSource (which can be a WindowProxy, MessagePort, or ServiceWorker object) representing the message emitter.

MessageEvent.ports Read only

An array of MessagePort objects representing the ports associated with the channel the message is being sent through (where appropriate, e.g., in channel messaging or when sending a message to a shared worker).

Examples

This example shows a shared worker file — when a connection to the worker occurs from a main thread via a MessagePort, the onconnect event handler fires. The event object is a MessageEvent.

The connecting port can be referenced through the event object's ports parameter; this reference can have an onmessage handler attached to it to handle messages coming in through the port, and its postMessage() method can be used to send messages back to the main thread using the worker.

js
self.onconnect = (e) => {
  const port = e.ports[0];

  port.onmessage = (e) => {
    const workerResult = `Result: ${e.data[0] * e.data[1]}`;
    port.postMessage(workerResult);
  };

  port.start();
};

For a complete running example, see our Basic shared worker example (run shared worker.)

addEventListener equivalent

You could also set up an event handler using the addEventListener() method:

js
self.addEventListener("connect", (e) => {
  const port = e.ports[0];

  port.onmessage = (e) => {
    const workerResult = `Result: ${e.data[0] * e.data[1]}`;
    port.postMessage(workerResult);
  };
});

Specifications

Specification
HTML
# event-workerglobalscope-connect
HTML
# handler-sharedworkerglobalscope-onconnect

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
connect event
Chrome – Full support
Chrome 4 (Release date: 2010-01-25)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 29 (Release date: 2014-04-29)
footnote
footnote Before version 65 the data property of the event object was null; it is now initialized to an empty string, as per spec.
Opera – Full support
Opera 10.6 (Release date: 2010-07-01)
footnote Full support
Safari – No support
Safari 5 – 6 (Release date: 2010-06-07)
footnote Removed in 7 and later
Safari – Full support
Safari 16 (Release date: 2022-09-12)
footnote Full support
Chrome Android – Preview support
Chrome Android 148 (Release date: 2026-05-05)
footnote Preview browser support
Firefox for Android – Full support
Firefox for Android 29 (Release date: 2014-04-29)
footnote
footnote Before version 65 the data property of the event object was null; it is now initialized to an empty string, as per spec.
Opera Android – Full support
Opera Android 11 (Release date: 2011-03-22)
footnote Full support
Safari on iOS – No support
Safari on iOS 5 – 6 (Release date: 2011-10-12)
footnote Removed in 7 and later
Safari on iOS – Full support
Safari on iOS 16 (Release date: 2022-09-12)
footnote Full support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – Preview support
WebView Android 148 (Release date: 2026-05-05)
footnote Preview browser support
WebView on iOS – No support
WebView on iOS 5 – 6 (Release date: 2011-10-12)
footnote Removed in 7 and later
WebView on iOS – Full support
WebView on iOS 16 (Release date: 2022-09-12)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
In development. Supported in a pre-release version.
In development. Supported in a pre-release version.
No support
No support
See implementation notes.
Has more compatibility info.

See also