mattgodbolt / seasocks

Simple, small, C++ embeddable webserver with WebSockets support
BSD 2-Clause "Simplified" License
735 stars 119 forks source link

contiguous stream of data ? #156

Open journeytosilius opened 3 years ago

journeytosilius commented 3 years ago

New to websockets here. I am developing an app that receives a data through a ZMQ socket every second, then processes it and prepares it in JSON format, and my goal was to be able to stream this real time data contiguously serving it through websockets, so a React app can grab it and make visuals with it.

My questions:

Thanks !

mattgodbolt commented 3 years ago

is it possible to stream JSON data regardless of if a client is connected to the endpoint, and once the client connects, it starts doing something with the data the server is streaming ?

With no clients, there's nothing to stream to. You can write code to buffer those JSON documents though.

can you add multiple / routes, and run them on separate threads ? My goal is to add a / route for each ZMQ socket receiving data, to later stream them on different tabs on the React app.

Not easily. Seasocks is inherently single-threaded, but typically if you have work on other threads you can kick them to the seasocks thread to send.

In similar designs I've used before I end up having a buffer of messages, and then onConnect I send them all to new clients. On a new message I add to the buffer (for later replay), and then loop over all currently connected instances to send to them.

If those messages are generated on another thread I'll use seasocks.execute() to ensure the enqueing/loop over all instances happens on the Seasocks thread, while everything else happens on my other threads.

If I get time I'll try and write an example that does this kind of thing - it's the kind of Seasocks is designed for. (originally, outputting streaming market data from a C++ trading app for visualisation)