# Problems with LSLClient in mne\_realtime

**URL:** <https://mne.discourse.group/t/problems-with-lslclient-in-mne-realtime/7455>\
**Category:** Support & Discussions\
**Tags:** eeg, mne-realtime\
**Created:** [August 29, 2023, 11:56am UTC](https://mne.discourse.group/t/problems-with-lslclient-in-mne-realtime/7455 "2023-08-29T11:56:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MMostafa-Hub](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mmostafa-hub/32/2983_2.png) [@MMostafa-Hub](https://mne.discourse.group/u/MMostafa-Hub)\
**Post date:** [August 29, 2023, 11:56am UTC](https://mne.discourse.group/t/problems-with-lslclient-in-mne-realtime/7455/1 "2023-08-29T11:56:24Z")

</div>

I’ve been working on a system that establishes a seamless connection between my primary PC, equipped with the `MNE` and `Python` tools (Master), and my laptop. The laptop runs the openViBE Acquisition server, which is linked to a BCI headset (Slave).

My primary goal is to enable the Slave system to transmit EEG data to the Master system through the LSL (Lab Streaming Layer) protocol. Once received, this data will undergo a series of processing steps utilizing the `realtime_mne` library.

Here are the specifications I’ve set for the Acquisition server:

![](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/b/b25fbc7aa3093a175f08dc75e4f79b70b69c9c80.png)

I’ve followed the guidance in the [MNE documentation](https://mne.tools/mne-realtime/auto_examples/plot_lslclient_rt.html) to visualize real-time EEG data using LSL. I made some modifications to the provided code, removing the MockLSLStream component. The resulting code is shown below:

```python
# Author: Teon Brooks <teon.brooks@gmail.com>
#
# License: BSD (3-clause)
import matplotlib.pyplot as plt
from mne_realtime import LSLClient

print( __doc__ )

# this is the host id that identifies your stream on LSL
host = 'eeg_stream'
# this is the max wait time in seconds until client connection
wait_max = 5

# For this example, let's use the mock LSL stream.
_, ax = plt.subplots(1)
n_epochs = 5

# main function is necessary here to enable script as own program
# in such way a child process can be started (primarily for Windows)
if __name__ == ' __main__':
    with LSLClient(info='eeg', host=host, wait_max=wait_max) as client:
        client_info = client.get_measurement_info()
        sfreq = int(client_info['sfreq'])

        # let's observe ten seconds of data
        for ii in range(n_epochs):
            print('Got epoch %d/%d' % (ii + 1, n_epochs))
            plt.cla()
            epoch = client.get_data_as_epoch(n_samples=sfreq)
            epoch.average().plot(axes=ax)
            plt.pause(1.)
        plt.draw()
print('Streams closed')

```

and this what was printed in the console until it thrown an traceback:

```python
Client: Waiting for server to start
Looking for LSL stream eeg_stream...
eeg_stream not found in streams: ['(0xe92637cf, 0x5efba8a2)', '(0x028b313d, 0x9e9fd28f)']
Looking for LSL stream eeg_stream...
eeg_stream not found in streams: ['(0xe92637cf, 0x5efba8a2)', '(0x028b313d, 0x9e9fd28f)']
Looking for LSL stream eeg_stream...
eeg_stream not found in streams: ['(0xe92637cf, 0x5efba8a2)', '(0x028b313d, 0x9e9fd28f)']
Looking for LSL stream eeg_stream...
eeg_stream not found in streams: ['(0xe92637cf, 0x5efba8a2)', '(0x028b313d, 0x9e9fd28f)']
Looking for LSL stream eeg_stream...
eeg_stream not found in streams: ['(0xe92637cf, 0x5efba8a2)', '(0x028b313d, 0x9e9fd28f)']
Looking for LSL stream eeg_stream...
eeg_stream not found in streams: ['(0xe92637cf, 0x5efba8a2)', '(0x028b313d, 0x9e9fd28f)']
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Cell In[1], line 21
     18 # main function is necessary here to enable script as own program
     19 # in such way a child process can be started (primarily for Windows)
     20 if __name__ == ' __main__':
---> 21 with LSLClient(info='eeg', host=host, wait_max=wait_max) as client:
     22 client_info = client.get_measurement_info()
     23 sfreq = int(client_info['sfreq'])

File c:\Users\pc\Anaconda3\envs\EEGenv\lib\site-packages\mne_realtime\base_client.py:76, in _BaseClient. __enter__ (self)
     74 time.sleep(0.1)
     75 else:
---> 76 self._connection_error()
     78 if not self.info:
     79 self.info = self._create_info()

File c:\Users\pc\Anaconda3\envs\EEGenv\lib\site-packages\mne_realtime\lsl_client.py:114, in LSLClient._connection_error(self)
    111 pylsl = _check_pylsl_installed(strict=True)
    112 extra = (f' Available streams on {self.host} from resolve_streams():\n'
    113 f'{pylsl.resolve_streams()}')
--> 114 super()._connection_error(extra)

File c:\Users\pc\Anaconda3\envs\EEGenv\lib\site-packages\mne_realtime\base_client.py:85, in _BaseClient._connection_error(self, extra)
     84 def _connection_error(self, extra=''):
---> 85 raise RuntimeError(f'Could not connect to Client.{extra}')

RuntimeError: Could not connect to Client. Available streams on eeg_stream from resolve_streams():
[,]

```

Despite this, I successfully connected to the stream using the `pylsl` library. Here’s the snippet that worked for me:

```python
import pylsl

# Resolve the LSL stream
streams = pylsl.resolve_stream("name", "eeg_stream")
# Check if the stream is found
if len(streams) == 0:
    print("LSL stream not found")
    exit()

# Create an inlet to receive data from the LSL stream
inlet = pylsl.stream_inlet(info=streams[0])

# Start receiving data
while True:
    # Get a chunk of samples from the LSL stream
    samples, timestamps = inlet.pull_chunk()

    # Process the received samples
    for sample, timestamp in zip(samples, timestamps):
        print(sample)

```

This `pylsl` script flawlessly printed out the received EEG data.

I’m eager to hear your thoughts and insights on resolving the issue with the `realtime_mne` code. Your assistance is greatly appreciated!

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [August 29, 2023, 2:05pm UTC](https://mne.discourse.group/t/problems-with-lslclient-in-mne-realtime/7455/2 "2023-08-29T14:05:13Z")

</div>

I had a quick look to the LSLClient [code](https://github.com/mne-tools/mne-realtime/blob/main/mne_realtime/lsl_client.py), and it looks like the stream is resolved by `source_id` and not by `name`. Thus, the `host` argument should correspond to the `source_id` of the LSL stream you are trying to connect to.

Mathieu

---

<div class="post-metadata">

**Author:** ![MMostafa-Hub](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mmostafa-hub/32/2983_2.png) [@MMostafa-Hub](https://mne.discourse.group/u/MMostafa-Hub)\
**Post date:** [August 29, 2023, 3:00pm UTC](https://mne.discourse.group/t/problems-with-lslclient-in-mne-realtime/7455/3 "2023-08-29T15:00:43Z")

</div>

Okay, do you know how exactly know the `source_id` of the LSL stream?  
or should I make this change to MNE’s source code to accept the `name`

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [August 29, 2023, 3:25pm UTC](https://mne.discourse.group/t/problems-with-lslclient-in-mne-realtime/7455/4 "2023-08-29T15:25:16Z")

</div>

The `source_id` is an attribute in the `StreamInfo`, just like the `name`. There is a non-zero chance that it’s going to be empty…

```python
streams = pylsl.resolve_stream("name", "eeg_stream")
sinfo = streams[0]
print (sinfo.source_id())

```

I think the code snippet above should work?

Mathieu
