Skip to content

Support for Parallel SSE Connections #16

@davpate

Description

@davpate

Hi there! Been testing out the module and gotten it working using the build available in #15 with xk6 to connect to a single SSE endpoint, but when I'm trying to expand that to include multiple SSE endpoints the connection appears to "block" the Event Loop until client.close() is called.

Any recommendations on what I might be doing wrong? Might just be that the module isn't setup for that at this time, I tried looking around but I'm not very experienced with Go/k6 extension development so not sure where to start with a PR to add that functionality yet.

Here's a quick test script of what I'm looking for. In this I'd like both of the SSE connections to occur in parallel as my app will be getting data from multiple SSE endpoints at once. In the example below you'll notice that the connection isn't made to url2 until the first SSE connection is closed due to an event.id value of 4 being sent by https://echo.websocket.org/.sse. Given this my console output looks like this:

Current Console Output

INFO[0000] connected                                     source=console
INFO[0000] event id=1, name=server, data=1781505b56ee58  source=console
INFO[0000] event id=2, name=request, data=GET /.sse HTTP/1.1  source=console
INFO[0001] event id=3, name=time, data=2024-08-06T16:01:21Z  source=console
INFO[0002] event id=4, name=time, data=2024-08-06T16:01:22Z  source=console
INFO[0002] closing connection                            source=console
INFO[0002] started sse1                                  source=console
INFO[0003] connected                                     source=console
INFO[0003] event id=, name=, data={"testing":true,"sse_dev":"is great","msg":"It works!","now":1722960083350}  source=console
INFO[0005] event id=, name=, data={"testing":true,"sse_dev":"is great","msg":"It works!","now":1722960085350}  source=console

You can see from the above that the call to sse.open(...) appears to be blocking the event loop as the next line console.log('started sse1') isn't executed until after the client.close() function is called.

Example Script

import sse from "k6/x/sse"

export default function () {
  const url1 = "https://echo.websocket.org/.sse"
  const url2 = "https://sse.dev/test"
  const params = {
    method: 'GET',
    headers: {
      "Authorization": "Bearer XXXX"
    },
    tags: {"my_k6s_tag": "hello sse"}
  }

  const response1 = sse.open(url1, params, handleEvents);
  console.log('started sse1');
  const response2 = sse.open(url2, params, handleEvents);
  console.log('started sse2');
}

function handleEvents(client) {
  client.on('open', function open() {
    console.log('connected')
  })

  client.on('event', function (event) {
    console.log(`event id=${event.id}, name=${event.name}, data=${event.data}`)
    if (parseInt(event.id) === 4) {
      console.log('closing connection');
      client.close()
    }
  })

  client.on('error', function (e) {
    console.log('An unexpected error occurred: ', e.error())
  })
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions