Skip to content

Events

Legacy v1 docs

v1 is on security fixes only. See v2 docs for the current release.

v1 uses a custom CustomEvent-based system. Listen with device.on(eventName, handler). The handler receives the native CustomEvent; payload is always in event.detail.

javascript
arduino.on("serial:connected", (event) => {
  console.log("connected!", event.detail);
});

Device events

EventDetailDescription
serial:connectingnullEmitted when the connection process starts
serial:connectednullEmitted when the device is fully connected and ready
serial:disconnectedobject | nullEmitted when the device disconnects (detail may carry an error message)
serial:need-permissionnullUser must interact with the browser to grant port access
serial:unsupportednullWeb Serial API is not available in this browser
serial:reconnect{}Emitted on a connection timeout when still in connect phase
serial:messagedevice-specificEmitted from your serialMessage() implementation
serial:corrupt-message{ code }Emitted from your serialCorruptMessage() implementation
serial:sent{ action, bytes }Emitted after bytes are written to the port
serial:timeout{ message, action, code, bytes }Emitted when a queued command does not receive a response in time
serial:soft-reload{}Emitted by softReload(); use it to reset your local state
serial:errorError | DOMExceptionAny uncaught error from connection or I/O
serial:lostnullEmitted when the port is unexpectedly removed/closed
debug{ type, data }Internal debug event (only fired when debug mode is active)

Registry event

EventDetailDescription
change{ devices, dispatcher }Fired on Devices.instance whenever context state changes

Socket events (window-level)

When socket: true is set in the constructor, these events are dispatched on window:

EventDescription
serial:socket:connectedSocket.io connection established
serial:socket:disconnectedSocket.io connection lost

Event detail shapes

serial:timeout

typescript
{
  message: string;   // "Operation response timed out."
  action: string;    // the queue action (e.g. "connect", "hi")
  code: Uint8Array | string[] | null;
  bytes: Uint8Array | string[];
  no_code: number;
}

serial:sent

typescript
{
  action: string; // the queue action
  bytes: Uint8Array; // the bytes that were written
}

serial:disconnected

typescript
{
  error?: string;    // reason, e.g. "Port is closed, not readable or writable."
}

Released under the MIT License.