# syncWithHeadsetClock

This method lets you synchronize the monotonic clock of your application with the monotonic clock of Cortex. It is an important step if you want to [inject markers](/cortex-api/markers/injectmarker.md) with a high precision.

Cortex uses a different clock for each headset and it initializes this clock when the headset is connected by Bluetooth or USB dongle. This means that:

* You must call “syncWithHeadsetClock” after the headset is connected, not before.
* You must provide a headset id.
* If the headset is disconnected and connected again, then you must call this method again.

The simplest solution is to call this method just before [creating a record](/cortex-api/records/createrecord.md).

## Use Case

Cortex sets the timestamps of each EEG sample using the [monotonic clock](https://itnext.io/as-a-software-developer-why-should-you-care-about-the-monotonic-clock-7d9c8533595c) of the computer. It is more reliable than using the system clock, because the system clock is subject to unpredictable variations, notably because of the [NTP](https://en.wikipedia.org/wiki/Network_Time_Protocol) synchronization.

When you [add a marker](/cortex-api/markers/injectmarker.md) to a record, Cortex uses the parameter "time" of your request to associate the marker with the EEG sample that has the closest timestamp. So if you want to have accurate markers, you should set the time of your markers using the monotonic clock, not the system clock.

Most programing languages provide a function to get a monotonic time. But in general, the point of origin of this time is undefined. So you need to synchronize the monotonic clock of your application with the monotonic clock used by Cortex. This is the purpose of this Cortex API method.

## Parameters

This method takes 3 parameters:

<table><thead><tr><th width="182.11488455630382">Name</th><th width="150">Type</th><th width="150">Required</th><th width="231.31789407778498">Description</th></tr></thead><tbody><tr><td>headset</td><td><code>string</code></td><td>yes</td><td><p>The headset id.</p><p>Use <a href="/pages/-LfsLkJGkiKvzhlBEHU_">queryHeadset</a> to list available headsets.</p></td></tr><tr><td>monotonicTime</td><td><code>number</code></td><td>yes</td><td><p>The monotonic time of your application. The unit is in seconds. The origin can be anything.</p><p>You should get this time right before you call this API.</p></td></tr><tr><td>systemTime</td><td><code>number</code></td><td>yes</td><td><p>The system time of your application. It must be the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970 UTC.</p><p>You should get this time right before you call this API.</p></td></tr></tbody></table>

## Result

The result is a JSON object with these fields:

| Name       | Type     | Description                                                                                                                                 |
| ---------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| headset    | `string` | The headset id.                                                                                                                             |
| adjustment | `number` | <p>The difference between the monotonic clock of your application and the monotonic clock of the headset.</p><p>The unit is in seconds.</p> |

To synchronize your clock, you must add the “adjustment” to your monotonic times. If the “adjustment” is positive, then it means your clock is late compared to the Cortex clock. If the “adjustment” is negative, then your clock is ahead of the Cortex clock.

## Example

{% tabs %}
{% tab title="Request" %}

```json
{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "syncWithHeadsetClock",
  "params": {
    "headset": "INSIGHT-587XX4BB",
    "monotonicTime": 321.445,
    "systemTime": 1629978090.302
  }
}
```

{% endtab %}

{% tab title="Response" %}

```json
{
  "id": 1,
  "jsonrpc": "2.0",
  "result": {
    "adjustment": 1629977768.86108,
    "headset": "INSIGHT-587XX4BB"
  }
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://emotiv.gitbook.io/cortex-api/headset/syncwithheadsetclock.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
