Skip to content

Commit c0f1f0a

Browse files
committed
Event trigger blog 1st draft
1 parent 3b05669 commit c0f1f0a

1 file changed

Lines changed: 82 additions & 13 deletions

File tree

  • content/post/2026-01-09-EventTriggers

content/post/2026-01-09-EventTriggers/index.md

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ authors:
44
categories:
55
- Reality Bending Lab
66
- University of Sussex
7-
date: "2026-01-09"
7+
date: "2026-01-11"
88
image:
99
caption: ''
1010
placement: 0
1111
title: "How to send event triggers to Lab Streaming Layer from JsPsych"
12-
subtitle: "Learn how to set up DataPipe to collect and save data in OSF, including creating an OSF project, linking it to DataPipe, configuring data collection, and saving data from an experiment hosted on GitHub."
13-
summary: "Learn how to set up DataPipe to collect and save data in OSF, including creating an OSF project, linking it to DataPipe, configuring data collection, and saving data from an experiment hosted on GitHub."
12+
subtitle: "Understand the steps involving in setting up event triggers for your JsPsych experiments, including creating the bridge to Lab Streaming Layer, ensuring synchronisation with other signals and highly accurate timestamps."
13+
summary: "Understand the steps involving in setting up event triggers for your JsPsych experiments, including creating the bridge to Lab Streaming Layer, ensuring synchronisation with other signals and highly accurate timestamps."
1414
tags:
1515
- LSL
1616
- Data Collection
@@ -25,26 +25,95 @@ Hello there! 👋 Let's learn how to send event triggers to Lab Streaming Layer
2525

2626
Lets start with some basics!
2727

28-
IDEAS:
29-
30-
- add image of lux marker and show diagram from validation experiments to illustrate that it's comparatively the 'best' in terms of consistency of delay making it yield more accurate data
31-
32-
- You can find a simple example of this in action from our recent validation experiments, which compared when the event triggers (referred to as 'markers') picked up the screen changing from white to black, to the Bitalino LUX: <https://github.com/OliverACollins/muse-athena-test/tree/main/blackwhite>.
33-
3428
## What does this mean and when is this useful?
3529

3630
Lab streaming layer (LSL) is a system used to receive, synchronise and stream signals from multiple inputs during experiments. LSL is designed to help researchers easily compare their data across multiple technologies, as time synchrony is integral for making valid comparisons.
3731

3832
When collecting data on stimulus response during experiments, it's important that the stimulus onset is recorded precisely, especially if this is mapped onto physiological responses as this has implications on how we interpret our data. One good example of a use case is that you have a Muse headband or any other device that can scream via LSL, and you want to precisely mark events in it.
3933

40-
Event triggers are coded into JsPsych online experiments to accurately mark events, such as when a stimulus appeared on the screen.
34+
Event triggers are coded into JsPsych online experiments to accurately mark events, such as when a stimulus appeared on the screen. We have found this method to yield the most precise timestamps of events, compared to alternative methods such as using the Bitalino LUX.
4135

4236
This tutorial will explain how to set this up for an experiment situated on GitHub, although you can adapt this for your hosting platform.
4337

38+
This blog will help you understand the set-up for event triggers. For an example of this in action, refer to <https://github.com/OliverACollins/muse-athena-test/tree/main/blackwhite>. This experiment recorded markers on a screen turning from white to black- you may want to follow along with lsl_bridge.py and blackwhite_jspsych.html [^1] for the full implementation.
39+
40+
[^1]: Special shoutout to our placement student Oliver Collins for preparing these scripts!
41+
4442
## How to set up event triggers
4543

46-
1. **Create the LSL bridge in your repository:**
44+
#### Requirements:
45+
46+
- **Your experiment is in-person:** In order to use these event triggers, you will need to run your experiment on a local host server, which will need to be manually set up for each trial. Therefore, this setup is intended for in-person experiments that are led by a researcher to set up the participant's screen.
47+
48+
- **The researcher and the participant(s) each have a computer:** The participant's computer will send the markers to the researcher's computer, which will be used to record the events (and all other signals using LSL) during the trial.
49+
50+
#### The LSL bridge script:
51+
52+
The LSL bridge will send the markers to LSL- it 'listens' for messages from the browser that the participant is doing the experiment from, and converts them into 'markers' for your recording software (such as LabRecorder) will receive.
53+
54+
1. Configuration
55+
56+
- **Imports**: Load in standard python libraries for creating a web server (`http.server`, `urllib`) and the `mne_lsl` library to handle the data streaming.
57+
58+
- **Configure Variables**: Name the event trigger stream so you can find it in LabRecorder, e.g. `LSL_STREAM_NAME = "jsPsychMarkers"`.
59+
60+
- Add `SERVER_HOST = "0.0.0.0"` into the script to tell the server to listen to all available network interfaces, allowing the participant's computer to communicate with the researcher's.
61+
62+
- Specify the port for the html script to go to e.g. `SERVER_PORT = 5000`. You will add this port into the html script that holds the online experiment to, in order to send the experiment to this python script.
63+
64+
2. Create the LSL outlet for event triggers
65+
66+
- Define the metadata for the stream: `info = StreamInfo( name=LSL_STREAM_NAME...`
67+
68+
- Create the outlet object that will push data out to the network: `outlet = StreamOutlet(info)`.
69+
70+
3. Create the request handler to define what happens when the participant's browser contacts the server.
71+
72+
- The sync route:
73+
74+
- `if path == "/sync":`: Checks if the browser is asking to synchronize clocks.
75+
76+
- `ts = local_clock()`: Grabs the current high-precision time from the LSL clock on the Recording Machine.
77+
78+
- `self.wfile.write(...)`: Sends this timestamp back to the browser. The browser needs this to calculate the time difference (offset) between the two computers.
79+
80+
- The marker route:
81+
82+
- `elif path == "/marker"`: Checks if the browser is trying to send an event marker.
83+
- It extracts `value` (the marker name, e.g., "1") and `ts` (the timestamp calculated by the browser) from the URL parameters.
84+
- `outlet.push_sample([value], ts)`: This is the most important line. It injects the marker into the LSL stream *using the timestamp provided by the browser*. This ensures that even if there is network lag, the timestamp recorded in the EEG data remains accurate to when the event actually happened on the participant's screen.
85+
86+
4. Run the Server
87+
88+
- `run_server()`: Starts the HTTP server and prints a confirmation message that it is ready for LabRecorder.
89+
- `threading.Thread(...)`: Runs the server in a separate thread so it doesn't block the main Python process.
90+
91+
#### Synchronise your event triggers in the html script:
92+
93+
This is 'promise-based', meaning it is coded to wait until it receives a signal from the participant's computer. This javascript code is designed to track the exact time it is on the participant's computer and send markers precisely aligned to that time.
94+
95+
1. Synchronisation
96+
97+
- `var lslBaseTime = null`: A variable to store the calculated time difference between the two computers.
98+
99+
- The function `syncLSL() {...}` can be looped three times to get an average reading.
100+
101+
- Get the time of the marker: `fetch("http://.../sync")`
102+
103+
- Record `startPerf` (when the request left) and `endPerf` (when the answer came back).
104+
105+
- You can assume the server received the message exactly halfway between start and end (`perfMid`).
106+
107+
- Calculates the difference between the browser's clock and the LSL clock: `offsets.push(lslTime - perfMid / 1000)`.
108+
109+
- Finally, average these offsets into `lslBaseTime`. Now the browser knows how to convert its own time to LSL time.
110+
111+
2. Sending markers
112+
113+
- Safety check in case sync fails: `if (lslBaseTime === null)` can be coded to send markers based on the timing from the participant's computer without synchronisation, which is less accurate but better than nothing.
114+
115+
- Code the mathematical logic to account for the offset in time for the recording computer to receive the marker, so the generated timestamp aligns with the recording computer's timestamp: `var ts = lslBaseTime + performance.now() / 1000`.
47116

48-
2. **Synchronise your event triggers in the html script of your experiment:**
117+
- Send the marker name and this calculated timestamp to the python bridge: `fetch(url)`.
49118

50-
## How to record event triggers during your experiment
119+
You are now ready to record event triggers during your experiment. For a guide on how to set this up, you can refer to the README.md file of: <https://github.com/OliverACollins/muse-athena-test>.

0 commit comments

Comments
 (0)