files/en-us/web/api/serialport/connect_event/index.md
{{APIRef("Web Serial API")}}{{SecureContext_Header}}{{SeeCompatTable}}{{AvailableInWorkers("window_and_dedicated")}}
The connect event of the {{domxref("SerialPort")}} interface is fired when the port connects to the device.
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 {{domxref("Serial.requestPort()")}} call:
This event bubbles to the instance of {{domxref("Serial")}} that returned this interface. The event.target property refers to the {{domxref('SerialPort')}} object that bubbles up.
For more information, see Event bubbling.
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.
addEventListener("connect", (event) => { })
onconnect = (event) => { }
A generic {{domxref("Event")}}.
The {{domxref("Serial.requestPort()")}} method returns a {{jsxref("Promise")}} that resolves with a {{domxref("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
});
The connect event bubbles up to the {{domxref("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}}
{{Compat}}