ExtendableMessageEvent.source

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018.

sourceExtendableMessageEvent インターフェイスの読み取り専用プロパティで、メッセージの送信元の Client オブジェクトへの参照を返します。

ClientServiceWorkerMessagePort の何れかのオブジェクトです。

次のコードをサービスワーカー内で使用して、PushMessageData を介して受信したデータをチャンネルメッセージを介してメインコンテキストに送信することにより、プッシュメッセージに応答する場合、onmessage のイベントオブジェクトは ExtendableMessageEvent になります。

js
let port;

self.addEventListener("push", (e) => {
  const obj = e.data.json();

  if (obj.action === "subscribe" || obj.action === "unsubscribe") {
    port.postMessage(obj);
  } else if (obj.action === "init" || obj.action === "chatMsg") {
    port.postMessage(obj);
  }
});

self.onmessage = (e) => {
  console.log(e.source);
  port = e.ports[0];
};

仕様書

Specification
Service Workers
# extendablemessage-event-source

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
source

Legend

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

Full support
Full support
No support
No support

関連情報