Callbacks & errors
Every method and callback of a measurement, what each callback receives, and every error code with a fix.
A measurement reports progress, live values, the result and errors through callbacks. Each
on* method returns the measurement, so calls chain:
m.onStage(showStage)
.onRealtime(showHeartRate)
.onCoach(showHint)
.onResult(saveResult)
.onError(showError);- Each event holds one callback. Registering again replaces the previous one.
- Register callbacks before or after
start(). - There is no unsubscribe or destroy.
stop()ends the measurement and releases the camera.
Methods
| Method | Returns | Description |
|---|---|---|
init({ keyId, secret }) | measurement | On-device only. Sets the key ID added to the result; secret is optional and needed only for submit(). Synchronous. |
start() | Promise<void> | Opens the camera, loads the SDK and starts capturing. Resolves once capture has started. Never rejects — failures go to onError. Does nothing if the measurement is already running. |
stop() | Promise<void> | Never rejects. On-device: ends the measurement, computes the result and fires onResult; a stop during startup cancels without a result. Cloud: ends early and resolves after the result arrives. |
submit(result, { includeAdvanced }) | Promise<MeasurementResult> | On-device only. Stores the result with POST /sdk/v1/measurements and returns the stored measurement. Requires secret. |
onRealtime(cb) | measurement | Live heart rate and signal quality. |
onStage(cb) | measurement | Progress stage. |
onCoach(cb) | measurement | Cloud only. Hint for the user. |
onDeliveryFallback(cb) | measurement | Cloud with delivery: 'auto'. The SDK switched to upload. |
onResult(cb) | measurement | Final result. |
onError(cb) | measurement | Error. |
A cloud measurement ends by itself at the session's duration. An on-device measurement runs
until you call stop().
onRealtime
m.onRealtime((update: {
heartRate: number | null;
quality: { level: 'good' | 'fair' | 'poor'; score: number | null };
}) => {});| Mode | When it fires |
|---|---|
| Cloud | Up to twice per second, only while the signal is good enough. When the signal is poor, updates pause — show the onCoach hint instead. |
| On-device | On every update during capture. heartRate is null while the signal is poor. |
Live values are provisional. Show the values from onResult as the outcome. quality.score
is between 0 and 1 — see Signal quality.
onStage
onStage receives a stage name from start() until the result arrives.
| Stage | Mode | Meaning | Suggested text |
|---|---|---|---|
requestingCamera | both | Waiting for camera permission and the camera to open. | Allow camera access |
loadingModels | both | Loading the SDK runtime. | Loading… |
preparingWorkers | on-device | Preparing the measurement. | Preparing… |
initializingSdk | on-device | Starting the measurement. | Starting… |
connecting | cloud | Connecting to Neccessory cloud. Skipped with delivery: 'batch'. | Connecting… |
ready | both | Capture has started. | Hold still and look at the camera |
uploading | cloud | Uploading the measurement data — with delivery: 'batch', or after a switch to upload with 'auto'. | Uploading… |
processing | cloud | Capture has ended; the result is being computed. | Calculating results… |
- On-device:
requestingCamera→loadingModels→preparingWorkers→initializingSdk→ready. - Cloud:
requestingCamera→loadingModels→connecting→ready→processing. With'batch', or after a switch to upload, the end isuploading→processing.
Treat an unknown stage as "still working".
onCoach
Cloud only. onCoach receives a hint for the user, or null when the problem is gone and
the hint should be cleared. Hints are already debounced — show them as they arrive.
| Reason | Meaning | Suggested message (EN) | Suggested message (RU) |
|---|---|---|---|
faceTooSmall | The face is too far from the camera or out of view. | Move closer to the camera | Придвиньтесь ближе к камере |
tooDark | The picture is too dark. | Find more light | Добавьте света |
tooBright | The picture is overexposed. | Move out of direct light | Уйдите от прямого света |
unstable | The user is moving too much. | Hold still | Не двигайтесь |
lowSignal | The signal is weak for another reason. | Keep your face in view and hold still | Держите лицо в кадре и не двигайтесь |
In on-device mode, prompt the user from quality.level in onRealtime.
onDeliveryFallback
Cloud with delivery: 'auto' only. Fires at most once per measurement, when the live
connection is lost and the SDK finishes the measurement by upload. Live updates stop,
onStage continues with uploading and processing, and onResult still fires.
| Field | Type | Description |
|---|---|---|
reason | string | Short code for your logs, such as socket_closed, connect_timeout or visibility_hidden. |
source | 'node' | 'local' | 'node': Neccessory cloud ended the live connection. 'local': the SDK ended it — the connection was lost, timed out or the tab was hidden. |
onResult
Fires once with the final measurement result.
| Cloud | On-device | |
|---|---|---|
| Stored | Already stored | Not stored — send it to your backend and store it with POST /sdk/v1/measurements |
id | Measurement ID | Absent until stored |
origin | 'cloud' | Stored as 'on_device' |
attested | true when the measurement is complete and its quality is usable | Stored as false |
When quality.level is 'poor', every metric is null. Check quality.usable before you
show values — see Signal quality.
onError
onError receives { code, message }. Map code to your own text; message is for logs.
In cloud mode, errors can arrive after start() has resolved — during capture or during
stop(). After an error the measurement is over: create a new one, in cloud mode with a
new session.
Error codes
| Code | Mode | Cause | What to do |
|---|---|---|---|
no_video | both | No video element. With the React hook, videoRef was not attached when start() was called. | Pass videoEl, or render the <video> before calling start(). |
load_failed | both | The camera could not be opened — permission denied, camera in use or absent — or the SDK runtime failed to load. | Ask the user to allow camera access and close other apps that use the camera. Check that your network and CSP allow neccessory.com and cdn.jsdelivr.net. |
start_failed | both | Startup failed or timed out — for example, in on-device mode the camera delivered no video within 8 seconds. | Retry. If it repeats, check the camera. |
core_unavailable | on-device | The browser cannot run the SDK. | Ask the user to update the browser or use a supported one, or use cloud mode. |
sdk_*, e.g. sdk_inference | on-device | Processing error during the measurement. | Start a new measurement. |
no_session_token | cloud | No session token. | Pass the whole session object from your backend. |
no_ws_url | cloud | The session has no wsUrl and delivery is not 'batch'. | Pass the whole session object. With a test key, use delivery: 'batch'. |
no_session_id | cloud | No session ID for the upload. | Pass the whole session object. |
no_frames | cloud | The camera delivered no video within 8 seconds. | Retry. Ask the user to close other apps that use the camera. |
cloud_unreachable | cloud, 'stream' | The live connection could not open within 8 seconds. A firewall, proxy or CSP blocks WebSockets. | Use delivery: 'auto' or 'batch'. Allow the session's wsUrl host in connect-src. |
stream_interrupted | cloud, 'stream' | The connection dropped, the tab was hidden, or the result did not arrive in time. | Start again with a new session. Use delivery: 'auto' to finish by upload instead. |
insufficient_signal | cloud | No usable data was captured — for example, the face was never in view. | Guide the user and start again with a new session. |
upload_failed | cloud, 'batch' or 'auto' | The upload or processing failed, or no result arrived within 180 seconds. | Start again with a new session. |
token_invalid, token_expired | cloud | The session token was rejected or is older than 5 minutes. | Create the session right before start(). |
session_already_used | cloud | The session was already used for a measurement. | Create a new session for every measurement. |
frame_rate_exceeded, duration_exceeded, idle_timeout, protocol_error, internal_error, cloud_error | cloud, 'stream' | Neccessory cloud ended the session. With 'auto', the SDK finishes most of these by upload instead. | Start again with a new session. |
submit() errors
submit() rejects with { code, message }. signAndSubmit() and signAndFetchMeasurement()
reject the same way.
| Code | Cause |
|---|---|
no_secret | init() was called without secret. |
API error code, e.g. validation_error, unauthorized, rate_limited | The API rejected the request — see Errors. |
HTTP status, e.g. "502" | The response was not an API response. |