r/javascript • • Nov 02 '26

serialport-rs for Node.js: Rust-backed serial ports for Node.js with parallel I/O and SerialPort-compatible APIs

https://github.com/DragonWork/node-serialport-rs

I recently ran into an issue with @serialport/bindings-cpp on Node.js 26.4, where serial reads could stall until unrelated JavaScript activity occurred.

While investigating the issue and contributing fixes upstream, I started experimenting with an alternative implementation using Rust and N-API, because upstream seems to be unmaintained for more than a year now.
The result is serialport-rs for Node.js, which uses the existing Rust serialport crate underneath and aims to preserve the familiar Node.js SerialPort API.

The implementation uses dedicated native I/O workers rather than relying on JavaScript-side polling. Initial benchmarks have also shown some promising latency improvements.

It’s available on npm if anyone is interested in trying it:
https://www.npmjs.com/package/serialport-rs

9 Upvotes

6 comments sorted by

1

u/ortegaj50 12d ago

Dedicated I/O workers should help avoid JS-side polling. How do they preserve read ordering and close/error behavior?

2

u/dragon-95 8d ago • Heartwarming

We use our own I/O threads, so waiting for serial data doesn’t tie up a thread in libuv’s worker pool or make JS poll. Ports can share a worker. Each port has one reader, so JS gets the bytes in order.

Calling `close()` cancels pending I/O and emits `close` once. If you unplug the device, that `close` event comes with `err.disconnected === true`.

1

u/gianluca_caruso-6 6d ago

When ports share a worker, what prevents one port waiting for input from delaying reads on another?

2

u/dragon-95 6d ago • Bravo

Each port has its own async task. When one port is waiting for input, its task yields, so the worker can handle reads on another port as soon as data is ready. A quiet port can stay open all day without holding up a busy one.

2

u/dragon-95 6d ago • Bravo

And: as you open more ports, we add workers up to the number of available CPU cores, capped at four. After that, new ports share the worker with the fewest ports. A port waiting for input yields, so other ports on that worker can still read.

3

u/Unable-Ad-9092 7d ago • Bravo Heartwarming

Thanks! Amazing library! Works fine even with termios2 / 3.25 MBps.