JavaScript client
This page shows how to talk to a go-ricochet server from TypeScript or JavaScript with the @go-ricochet/ricochet package (source). It describes version 0.1.0. The package is a port of the Go client, pkg/client, and its wire format, payload encryption and compression are byte-compatible with it.
Install
Section titled “Install”npm install @go-ricochet/ricochetThe package is ESM only and needs Node.js 22.18 or later. It brings js-libp2p 3 and the UDX transport, @go-ricochet/libp2p-udx, as dependencies.
Pick an entry point
Section titled “Pick an entry point”| Import | For | Adds |
|---|---|---|
@go-ricochet/ricochet/browser |
Browsers, Electron renderers | createRicochetNode() with WebSockets |
@go-ricochet/ricochet/node |
Node.js, Electron main process | createRicochetNode() with UDX and WebSockets, and resolveDnsAddrs() |
@go-ricochet/ricochet/core |
Any platform, with your own js-libp2p node | Nothing platform-specific |
@go-ricochet/ricochet on its own picks browser or node by your bundler’s or runtime’s export conditions, else core. Every entry exports RicochetClient and registers the /udx multiaddr codec, so js-libp2p can read the /udx addresses a server advertises.
The transport decides which servers you can reach:
- Browsers connect over WebSocket only, so the server must run the
websockettransport. A page served overhttps://needs a/tls/wsor/wssaddress, which means the server also needswebsocket_tlsor a TLS proxy in front. See Transport. - Node.js and Electron connect over UDX, the server’s default, or WebSocket. The UDX transport dials IP addresses only:
resolveDnsAddrs()replaces/dns4and/dns6parts with the addresses they resolve to.
Create the client
Section titled “Create the client”// Sketch: serverPeerId is the server's peer ID, printed at startup.import { RicochetClient, createRicochetNode } from '@go-ricochet/ricochet/node'
const { node, privateKey } = await createRicochetNode()const client = new RicochetClient(node, { servers: [{ peerId: serverPeerId, addrs: ['/ip4/203.0.113.10/udp/55223/udx/p2p/12D3KooW...'] }], privateKey // needed only to encrypt and decrypt})
// ... use the client ...
await client.close() // the node stays running; it is yoursawait node.stop()In a browser, import from @go-ricochet/ricochet/browser and give the server’s secure WebSocket address, such as /dns4/ricochet.example.com/tcp/443/tls/ws/p2p/12D3KooW.... In Node.js, createRicochetNode({ udxOnly: true }) leaves out WebSockets. A client does not need to listen: the server pushes notifications over the connection the client opened.
servers entries take peerId, addrs, priority and weight. Lower priority is tried first, as on the Go client; among equal priorities, higher weight goes first. A server that cannot be reached is passed over for the next. connectionTimeout (default 10 000 ms) and messageTimeout (default 30 000 ms) set the time limits.
If your app already runs js-libp2p, import RicochetClient from @go-ricochet/ricochet/core and pass your node instead. The node needs Noise and Yamux, and the app must share one copy of @multiformats/multiaddr 13.x with the package.
Send and retrieve
Section titled “Send and retrieve”// Sketch: client is the RicochetClient above; recipientPeerId is a peer ID string.import { Priority } from '@go-ricochet/ricochet/node'
const sent = await client.sendMessage(recipientPeerId, 'Hello!', { folderPath: 'inbox', // the default priority: Priority.Normal, persistent: false // removed once acknowledged})if (!sent.success) throw sent.error ?? new Error(sent.errorMessage)console.log(`Stored at ${sent.storedAtServer}`)
const messages = await client.retrieveMessages()for (const m of messages) console.log(new TextDecoder().decode(m.payload))await client.markDelivered(messages.map(m => m.messageId))Payloads are a Uint8Array or a string, sent as UTF-8. Retrieval never removes a message; markDelivered removes non-persistent messages and marks persistent ones seen, as on the Go client. A refused sendMessage is a result with success: false, status and error. Other failures throw: a refusal from the server is a ProtocolError with the server’s status, or a subclass such as ForbiddenError, NotFoundError or RateLimitedError.
Encryption and compression
Section titled “Encryption and compression”Pass privateKey when you create the client, then send with encrypt: true. The format is the Go client’s: NaCl box from the two Ed25519 identity keys, bound to the recipient, folder and message ID. Retrieved messages come back decrypted; a message with bound: false used the older format without the binding. compress: true LZ4-compresses payloads of 1 KB and more, before encryption. See Encryption & compression.
await client.sendMessage(recipientPeerId, payload, { encrypt: true, compress: true })Coverage compared with the Go client
Section titled “Coverage compared with the Go client”| Feature | Go pkg/client |
JavaScript RicochetClient 0.1.0 |
|---|---|---|
| Send one, batch, forward | SendMessage, SendMessages, ForwardMessage |
sendMessage, sendMessages, forwardMessage |
| Retrieve, paging, another peer’s mailbox | RetrieveMessages, RetrievePage, WithTargetPeer |
retrieveMessages, retrievePage, targetPeerId |
| Acknowledge, flags, expunge, delete | MarkDelivered, UpdateFlags, Expunge, DeleteMessages |
markDelivered, updateFlags, expunge, deleteMessages |
| Payload encryption (NaCl box) | WithEncryption |
encrypt: true with privateKey |
| LZ4 compression | WithCompression |
compress: true |
| Mailboxes and ACLs | Yes | createMailbox, updateMailboxConfig, deleteMailbox, grantAccess, revokeAccess, listACL, listMailboxes, getMailboxInfo |
| Server capacity | QueryCapacity |
queryCapacity |
| Documents: get, put, patch, head, delete, list, batch put | Yes | Yes, batch put as putDocuments |
| Document versions | No | getDocumentHistory, getDocumentVersion |
| Visibility and readers for documents, feeds and collections | Yes | Yes |
| Directory | Join, leave, browse, get entry | joinDirectory, leaveDirectory, browseDirectory, getDirectoryEntry |
| Feeds | Create, append, get, range, multi-feed, list, delete | Yes |
| Collections | Create, put, get, delete, list keys, query | Yes |
| Private mailbox notifications | RegisterNotificationHandler |
onNotification |
| Shared and public mailbox notifications (GossipSub) | No | No |
| Errors | Typed errors with the server’s status | ProtocolError and its subclasses, with the server’s status |
Testing
Section titled “Testing”The package’s repository has integration tests that run it against a local go-ricochet server over UDX, WebSocket and TLS WebSocket, and browser tests that run it in headless Chromium over WebSocket and TLS WebSocket. The tests build the server from source. They are not part of the npm package; see the README.