The a2a_client.js module provides a Node.js object-oriented API for a2a messaging, eliminating the need to shell out to the a2a CLI.
# Copy to your project
cp a2a_client.js /path/to/your/project/Requires Node.js 22+ (uses built-in node:sqlite — no npm dependencies).
const A2AClient = require('./a2a_client');
// Initialize client
const client = new A2AClient('my-project', 'alice');
// Send a message
const msgId = await client.send('bob', 'Hello Bob!');
// Receive messages (blocks up to 10 seconds)
const messages = await client.recv(10);
messages.forEach(msg => {
console.log(`${msg.sender}: ${msg.body}`);
});
// Broadcast
await client.send('all', 'Hello everyone!');
// Mark yourself done
await client.setStatus('done');All methods are async (return Promises).
Register this agent on the bus. Must be called before send/recv.
await client.register('researcher', 'Research things', 'node', null, true);Create the database and schema. No-op if already exists.
client.initProject();Get resolved project metadata: project name, database path, and whether the database file exists.
const info = client.projectInfo();
console.log(info.db, info.exists);Remove this agent from the bus.
await client.unregister();Send a message to a peer. Returns the message ID. Raises an error if the recipient is empty, ttlSeconds is not a positive number, or threadId is empty/whitespace.
const msgId = await client.send('bob', 'Hello', 3600);
const msgId2 = await client.send('bob', 'Follow-up', null, 'thread-1'); // with threadReceive messages addressed to this agent.
const messages = await client.recv(10); // Wait up to 10 secondsView recent messages without marking as read.
const recent = await client.peek(50);Alias for listPeers(). Returns all registered agents.
const peers = await client.list();Get roster of registered agents.
const peers = await client.listPeers();Get or set status. If arg is a valid status (active, idle, done,
blocked), sets the status and returns null. If arg is an agent ID,
returns that agent's status. If arg is omitted, returns this agent's
status.
await client.status('done');
const bobStatus = await client.status('bob');Update this agent's status (active/idle/done/blocked).
await client.setStatus('done');Check an agent's status.
const status = await client.getStatus('bob');Update this agent's last_seen timestamp. Useful for heartbeat/keep-alive signaling.
await client.touch();Alias for waitForMessages(). Block until N unread messages or timeout.
const success = await client.wait(3, 30);Block until N unread messages or timeout.
const success = await client.waitForMessages(3, 30);Search messages by content (case-insensitive).
const results = await client.search('important', 100);Get all messages in a thread.
const threadMessages = await client.thread("42");Get aggregated bus statistics.
const stats = await client.stats();
console.log(`Total messages: ${stats.messages}`);const A2AClient = require('./a2a_client');
async function main() {
const client = new A2AClient('project', 'coordinator');
// Broadcast task
await client.send('all', 'Please review the proposal');
// Wait for responses
await client.waitForMessages(3, 30);
// Collect reviews
const reviews = await client.recv();
console.log(`Got ${reviews.length} reviews`);
// Summarize
await client.send('all', 'Summary: All reviews received');
await client.setStatus('done');
}
main().catch(err => console.error(err));Delete the entire database file. All bus data is lost.
client.clear();Direct SQLite connections (no subprocess overhead):
- send(): ~5ms
- recv(): ~10ms per poll
- search(): ~20ms for 1000 messages
- CLIENT_API.md — Python client
- ADVANCED_PATTERNS.md — Patterns and optimization