And now I am trying to use the streaming api. For that I implement the backend.StreamHandler
// region Stream handling
func (d *App) SubscribeStream(_ context.Context, req *backend.SubscribeStreamRequest) (*backend.SubscribeStreamResponse, error) {
return &backend.SubscribeStreamResponse{
Status: backend.SubscribeStreamStatusOK,
}, nil
}
// PublishStream just returns permission denied in this case, since in this example we don't want the user to send stream data.
// Permissions verifications could be done here. Check backend.StreamHandler docs for more details.
func (d *App) PublishStream(context.Context, *backend.PublishStreamRequest) (*backend.PublishStreamResponse, error) {
return &backend.PublishStreamResponse{
Status: backend.PublishStreamStatusPermissionDenied,
}, nil
}
func (d *App) RunStream(ctx context.Context, req *backend.RunStreamRequest, sender *backend.StreamSender) error {
log.DefaultLogger.Info("WS request for path %s. Data ", req.Path, req.Data)
q := Query{}
err := json.Unmarshal(req.Data, &q)
if err != nil {
log.DefaultLogger.Error(err.Error())
q = Query{
TickInterval: 1,
UpperLimit: 100,
LowerLimit: 1,
}
}
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
ticker := time.NewTicker(q.TickInterval * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
// we generate a random value using the intervals provided by the frontend
randomValue := r.Float64()*(q.UpperLimit-q.LowerLimit) + q.LowerLimit
err := sender.SendFrame(
data.NewFrame(
"response",
data.NewField("time", nil, []time.Time{time.Now()}),
data.NewField("value", nil, []float64{randomValue})),
data.IncludeAll,
)
if err != nil {
log.DefaultLogger.Error("Failed send frame", "error", err)
}
}
}
}
// endregion
});
And try to call over
// How can I trigger the connection to func (d *App) RunStream(ctx context.Context, req *backend.RunStreamRequest, sender *backend.StreamSender)
// Without implementing custom datasource here ?
const backendSrv = getGrafanaLiveSrv().getStream({
namespace: 'ws',
path: 'test-test-app',
scope: LiveChannelScope.Stream,
});
All example that I found implements either a datasource or a scenes app with simple rest backend. But I can't find any example that implements a secenes app without a separate datasource.
I have scaffolded a demo scenes app with the command
and choose the option
The demo works so far so good. I can add further rest endpoints and call it from the frontend with
And now I am trying to use the streaming api. For that I implement the
backend.StreamHandler
And try to call over
All example that I found implements either a datasource or a scenes app with simple rest backend. But I can't find any example that implements a secenes app without a separate datasource.
Is there a standard way to comine both ?