SerialPort: connect 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 connect
event of the SerialPort
interface is fired when the port connects to the device.
Description
More specifically, the connect
event fires when the port becomes logically connected to the device after a user grants permission for a site to access the port following a Serial.requestPort()
call:
- In the case of a wired serial port, this occurs when the port is physically connected to the device, for example via USB.
- In the case of a wireless serial port (for example, Bluetooth RFCOMM), this occurs when the port makes one or more active connections to the device (for example via Bluetooth L2CAP channels).
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.
addEventListener("connect", (event) => {});
onconnect = (event) => {};
Event type
A generic Event
.
Examples
Notify when a specific port connects
The Serial.requestPort()
method returns a Promise
that resolves with a SerialPort
chosen by the user.
// Prompt user to choose a serial port
const port = await navigator.serial.requestPort();
port.addEventListener("connect", (event) => {
// notify that the chosen port is connected
});
Listening for any newly-connected ports
The connect
event bubbles up to the Serial
object where you can listen for any newly-connected ports.
navigator.serial.addEventListener("connect", (event) => {
// notify that a new port is available
// use `event.target` to refer to the newly-added port
});
Specifications
Specification |
---|
Web Serial API # dfn-connect |
Web Serial API # dom-serialport-onconnect |
Browser compatibility
BCD tables only load in the browser
See also
disconnect
event