How to connect streaming data source to websocket?

Hi Olivier,

I agree with you that the grafana documentation is far from adequate on this topic. I was also interested in using websocket for data streaming since it seems to work a bit better for “high” data rates (about 100 samples/s) compared to running it through a database like Redistimeseries first. I actually ended up having to sniff my own network traffic with Wireguard to figure out what the mysterious “influx data format” that the little piece of rushed documentation talks about looks like. Unfortunately I don’t have this capture at hand anymore, but I came up with this very quick and dirty piece op Python 3.7 script which can stream a sinewave to the Grafana websocket for test purposes:

import math
import time
import asyncio
import websockets

async def stream():
    async with websockets.connect("ws://community.grafana.com/api/live/push/sinewave_test", extra_headers={'Authorization': 'Bearer THETOKENSHOULDHAVEADMINRIGHTSIFIREMEMBERCORRECTLY'}) as websocket:
        i = 0
        while True:
            await websocket.send("test Sinewave=" + str((math.sin(math.radians(i))+1)*20+80) +" " + str(time.time_ns()+1000000000))
            if (i == 361):
                i = 0
            else:
                i = i + 15
            await asyncio.sleep(0.03)

asyncio.run(stream())

If I remember correctly, you can add more signals in the websocket.send() part in Name=Value format, maybe with a comma behind the previous one.

I hope this will get you started!

1 Like