Skip to main content

Node.js

The mojay npm package is a native binding to the Rust library, built with napi-rs. It calls the library directly: there is still no server or service to run. TypeScript definitions are included.

Install​

npm install mojay

You need Node.js 18 or later, on macOS or Linux (x64 or arm64).

Before the first release

Until the package is published on npm, build it from the repository. You need Rust (rustup) installed:

git clone https://github.com/SachaCR/mojay
cd mojay/bindings/node
npm install
npm run build

Then install it in your project with npm install /path/to/mojay/bindings/node. npm run build compiles for the architecture of the Node.js that runs it. With an x64 Node.js on an arm64 Mac (under Rosetta), the script prints the rustup target add command to run first.

Import it​

The package works with both module systems.

ESM
import { Db } from 'mojay'
CommonJS
const { Db } = require('mojay')

A first program​

quickstart.mjs
import { Db } from 'mojay'

const db = Db.open('./data')

// Create the topic the first time only.
try {
db.createTopic('orders', { partitions: 3 })
} catch (error) {
if (error.code !== 'MOJAY_TOPIC_EXISTS') throw error
}

// Produce: the key picks the partition, so a customer's orders stay in order.
const producer = db.producer('orders')
for (const customer of ['client-1', 'client-2', 'client-1']) {
const { partition, offset } = await producer.send({
key: customer,
value: JSON.stringify({ customer, total: 129.9 }),
headers: { source: 'quickstart' },
})
console.log(`sent ${customer} -> partition ${partition}, offset ${offset}`)
}
await producer.close()

// Consume as a member of the "billing" group, with automatic commit.
const consumer = db.consumer('billing', ['orders'], { autoCommitMs: 1000 })
for (const record of await consumer.poll(1000)) {
const order = JSON.parse(record.value.toString())
console.log(`p${record.partition}:${record.offset}`, order)
}
await consumer.close() // commits, then hands the partitions back to the group

Run it again: the consumer only prints the new messages, because the billing group committed its position when it closed.

Synchronous and asynchronous calls​

Opening things is synchronous; everything that may wait for the disk or for messages returns a Promise.

SynchronousReturns a Promise
Db.open, db.createTopic, db.listTopics, db.describeTopic, db.producer, db.consumerproducer.send, producer.flush, producer.close, consumer.poll, consumer.commit, consumer.assignment, consumer.rebalanceEvents, consumer.close

Each producer and each consumer has its own thread and its own queue of calls:

  • Nothing blocks Node.js. A poll that waits, or an fsync, runs on that thread. Neither the event loop nor Node's thread pool (used by fs and crypto) is occupied.
  • Calls run in order. Calls on the same producer or consumer run one at a time, in the order they were made, even if you do not await each Promise. Promise.all(messages.map((m) => producer.send(m))) writes the messages in array order.

API​

Db​

MethodPurpose
Db.open(path)Opens a data folder, creating it if needed. Several processes can open the same one.
db.createTopic(name, options?)Creates a topic. Throws MOJAY_TOPIC_EXISTS if it already exists.
db.listTopics()The topic names, sorted.
db.describeTopic(name){ name, partitions }, with for each partition partition, startOffset, readLimit, segments (a count) and sizeBytes.
db.producer(topic)A writer on the topic.
db.consumer(group, topics, options?)A member of the group group, subscribed to topics.

createTopic options, all optional:

OptionDefaultEffect
partitions1Number of partitions, fixed at creation.
segmentBytes64 MiBSize of a segment file before moving to the next one.
fsync'always''always': each message is on disk before it becomes readable. 'never': left to the OS. A number: one fsync every N milliseconds. See Durability.
retentionBytesnoneMaximum size of one partition, in bytes. Beyond it, the oldest segments are deleted.
retentionMsnoneMaximum age of a segment, in milliseconds since its last write.

Producer​

MemberPurpose
producer.topicThe topic name.
send(message)Sends a message. Resolves to { partition, offset }.
flush()Forces everything sent so far to disk, whatever the fsync policy.
close()Finishes the pending sends, then closes the producer.

A message is { key?, value?, headers?, partition?, timestamp? }:

  • key, value and header values accept a string (encoded as UTF-8) or a Buffer.
  • key picks the partition (murmur2, like Kafka). Without a key and without a partition, messages are spread round-robin.
  • partition forces a partition instead of the one chosen from the key.
  • Without a value, the message is a tombstone (null value).
  • timestamp is in milliseconds since the epoch, and defaults to the time of sending.
Throughput

The binding sends one message per send call. With fsync: 'always', each call waits for the disk, which limits a producer to a few hundred messages per second on a typical laptop SSD. If you need more, create the topic with an interval policy such as fsync: 100. See Durability.

Consumer​

MemberPurpose
consumer.groupThe group name.
poll(timeoutMs)Waits at most timeoutMs for messages. Resolves to a ConsumerRecord[], empty if nothing arrived in time.
commit()Commits the position of each owned partition: everything returned by poll so far is processed.
assignment()The partitions this member owns: [{ topic, partition }].
rebalanceEvents()The partitions received or given up since the last call: [{ kind: 'assigned' | 'revoked', topic, partition }].
close()Leaves the group: commits if auto-commit is on, then hands the partitions back to the other members.

Consumer options, all optional:

OptionDefaultEffect
startFrom'earliest'Where to start when the group has committed nothing for a partition: 'earliest' or 'latest'.
autoCommitMsnoneCommit automatically, at most every N milliseconds, at the start of each poll and on close. Without it, call commit() yourself.
maxPollBytes1 MiBApproximate maximum size of the messages returned by one poll.

A ConsumerRecord has topic, partition, offset, timestamp (ms), and key, value and headers as Buffers (key and value are absent when the message has none).

Offsets are numbers​

Offsets are JavaScript numbers. They are exact up to 253 messages per partition.

Close explicitly​

close() releases locks and files immediately. A producer or consumer that you forget is closed when the garbage collector collects it, with no guaranteed delay: until then, a consumer keeps its partitions and the rest of its group cannot read them.

await Promise.all([producer.close(), consumer.close()])

Any method called after close() fails with MOJAY_CLOSED.

Errors​

Every error is a regular Error with a stable code property, like Node's own errors:

try {
db.producer('orders')
} catch (error) {
if (error.code === 'MOJAY_UNKNOWN_TOPIC') console.log('create the topic first')
else throw error
}

Synchronous methods throw; asynchronous ones reject their Promise with the same kind of error. Rely on error.code in your code: error.message is meant for humans and may change. The full list of codes is in Errors.

Next steps​