SerialPort: disconnect event

Limited availability

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

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Note: This feature is available in Dedicated Web Workers.

The disconnect event of the SerialPort interface is fired when the port disconnects from the device.

Description

More specifically, the disconnect event fires when a port that was previously logically connected after a user granted permission for a site to access it (following a Serial.requestPort() call) is no longer connected.

Bubbling

This event bubbles to the instance of Serial that returned this interface. The event.target property refers to the SerialPort object that bubbles up.

For more information, see Event bubbling.

Syntax

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

js
addEventListener("disconnect", (event) => {});

ondisconnect = (event) => {};

Event type

A generic Event.

Examples

Notify when a specific port disconnects

Here the event listener is installed on a specific SerialPort object.

js
port.addEventListener("disconnect", (event) => {
  // notify that the port has become unavailable
});

Listening for any ports that become unavailable

The disconnect event bubbles up to the Serial object where you can listen for any ports that become unavailable.

js
navigator.serial.addEventListener("disconnect", (event) => {
  // notify that a port has become unavailable
  // use `event.target` to refer to the unavailable port
});

Specifications

Specification
Web Serial API
# dfn-disconnect
Web Serial API
# dom-serialport-ondisconnect

Browser compatibility

BCD tables only load in the browser

See also