Menu
#Writing a client
There is no BSON and no driver specification. A client opens a TCP connection and sends documents. The Node package and the viewer are the two existing clients. The viewer's client is the client/ crate of the ferrite_viewer repository, and the package is ferrite_js.
#1. Connect
Open a TCP socket to the server (default port 7878). If the server has TLS on, wrap it in TLS 1.3 first. A plain-text client gets a handshake error, and a server without TLS will not understand a TLS client. See TLS.
#2. Pick a format
- JSON lines: send each request as one line of JSON ending in
\n. The first line has to start with{. Read each reply as one line. - Binary: send
FE 66 72 31once, then for each message au32little endian length and the document. Replies have the same framing.
Anything else as the first byte closes the connection. Details and the tag table are in wire format. A client can support only JSON.
Replies come back in the order of the requests and have no ids. You can pipeline several requests on one connection and match replies by order.
#3. Log in
If the server has a token or any user, nothing is answered until you log in. Before that every line gets authentication required, and only short lines (4 KiB) are read. You have 10 seconds in all.
Token:
{"cmd":"auth","token":"<token>"}The reply is {"ok":true,"authenticated":true}. A wrong token gets authentication failed and the connection is closed.
User, with SCRAM-SHA-256 as in RFC 5802 and RFC 7677, no channel binding:
{"cmd":"scramStart","payload":"n,,n=alice,r=<client nonce>"}
{"cmd":"scramFinish","payload":"c=biws,r=<server nonce>,p=<client proof>"}The reply to each has the server's message in payload. Take the salt and iteration count from the first, and compute the proof with HMAC and PBKDF2 over SHA-256 as the RFC says. The reply to the second holds v=<server signature>. Check it: it proves the server has the keys. Passwords are ASCII only, so there is no SASLprep. A wrong password, an unknown name and a wrong token all get authentication failed and the connection is closed. A second login on a connection gets already logged in.
Five wrong logins from one address, each within a minute of the last, lock that address out for a minute. A client should not retry a refused login in a loop. See authentication.
#4. Choose a database
Every request takes a db, or the connection has a default from use:
{"cmd":"use","db":"shop"}A request with neither is a bad_request. A database is created by its first write, and a read of a missing one gives an empty result.
#5. Send requests, read replies
A success is {"ok":true,...}. A failure is:
{"ok":false,"code":"invalid","error":"unknown operator $x"}Use code to decide what to do, not the English message. The commands are in requests and replies.
#Error codes
| code | meaning |
|---|---|
bad_request |
not valid JSON, or not shaped like a request: a missing field, an unknown command |
invalid |
well formed but cannot be done: a bad operator, a field name with a ., $inc on a string |
duplicate |
an _id that is already taken |
duplicate_key |
a unique index refused the write |
unauthorized |
not logged in, wrong login, or the role is too low. The connection stays open for a refused request |
limit |
too long, too deeply nested, too many connections, a locked-out address, the memory limit |
conflict |
a transaction found that someone else wrote to what it used. Retry from begin |
read_only |
a write sent to a follower. Send it to the leader |
storage |
the disk or the log, not the client's doing |
A reply to a transaction write that ended the transaction also has "aborted":true. See errors.
#Value types
- JSON: ObjectId is
{"$oid":"<24 hex>"}, a date is{"$date":<ms>}. - Ints and floats are different types. A JSON number with a
.is a float, so write floats with a decimal point (1.0). On the binary wire sendlimit,skipand sort directions as ints. - Whole numbers above 2^53 are real ints on the server. A client that parses replies into JavaScript numbers will lose them.
- NaN and infinity are refused.
#Limits to respect
| limit | value |
|---|---|
| request line or binary message | 16 MiB (4 KiB before login) |
| values in a request | 1,000,000 |
| nesting | 100 levels |
| reply | 64 MiB. Send limit on find |
| connections | 1000 total, 100 per IP address |
| time for a request to arrive | 60 seconds after the last reply |
The server closes a connection that is silent for 60 seconds, so a pooled client should drop idle connections sooner (the package uses 30 s) or send traffic. See memory and limits.
#Streams and transactions
watchturns the connection into an event stream, and you stop it by closing the socket. See change streams.begin,commitandabortwork on one connection, so a client with a pool has to keep a transaction on one socket. A transaction is aborted if the connection closes or waits 30 seconds for its next request. See transactions.
#Reconnecting
Do not retry a write automatically after a lost connection: it may have gone through. Redo the login and use on a new connection, and fail the request that was in flight. A user whose grants or password changed has the connection closed at the next request, and logs in again.