The session.unsubscribe command of the session module cancels event subscriptions previously registered with session.subscribe, either by subscription ID or by event name.
Syntax
/* Using event name */
{
"method": "session.unsubscribe",
"params": {
"events": ["log.entryAdded"]
}
}
/* Using subscription ID */
{
"method": "session.unsubscribe",
"params": {
"subscriptions": ["a1b2c3d4-e5f6-7890-abcd-ef1234567890"]
}
}
Parameters
The params field contains one of the following fields:
events- : An array of one or more strings that specifies event names for canceling subscriptions.
Each string can be either a specific event name (for example,
"log.entryAdded") or a module name (for example,"log") that unsubscribes the client from all events in that module. Only global subscriptions can be removed using event names; those created usingcontextsoruserContextscannot be.
- : An array of one or more strings that specifies event names for canceling subscriptions.
Each string can be either a specific event name (for example,
subscriptions- : An array of one or more subscription IDs that specifies the subscriptions to cancel, including both global and context-scoped subscriptions.
Return value
The result field in the response is an empty object ({}).
Errors
invalid argument- : When unsubscribing using IDs, a subscription ID is not found.
When unsubscribing using event names, the
eventsarray is empty or omitted, or an event name does not match any active global subscription.
- : When unsubscribing using IDs, a subscription ID is not found.
When unsubscribing using event names, the
Examples
Unsubscribing using a subscription ID
With an active subscription, send the following message to cancel it by ID:
{
"id": 3,
"method": "session.unsubscribe",
"params": {
"subscriptions": ["c7b7b3a2-1f4b-4b4e-8a1f-2a3b4c5d6e7f"]
}
}
After successfully unsubscribing, the browser responds as follows:
{
"id": 3,
"type": "success",
"result": {}
}
Unsubscribing from an event globally using an event name
With a global subscription to log.entryAdded active, send the following message to stop receiving that event:
{
"id": 4,
"method": "session.unsubscribe",
"params": {
"events": ["log.entryAdded"]
}
}
After successfully unsubscribing, the browser responds as follows:
{
"id": 4,
"type": "success",
"result": {}
}
Unsubscribing from multiple events globally using event names
With global subscriptions active, send the following message to unsubscribe from all events in the log module and a specific event from the network module:
{
"id": 5,
"method": "session.unsubscribe",
"params": {
"events": ["log", "network.beforeRequestSent"]
}
}
After successfully unsubscribing, the browser responds as follows:
{
"id": 5,
"type": "success",
"result": {}
}
Specifications
Browser compatibility
See also
session.subscribecommandsession.newcommandsession.endcommand
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
