MCPcopy
hub / github.com/mudler/LocalAI / QueueSubscribeJSON

Function QueueSubscribeJSON

core/services/messaging/client.go:281–290  ·  view source on GitHub ↗

QueueSubscribeJSON creates a queue subscription that automatically unmarshals JSON messages. Invalid JSON messages are logged and skipped.

(c MessagingClient, subject, queue string, handler func(T))

Source from the content-addressed store, hash-verified

279// QueueSubscribeJSON creates a queue subscription that automatically unmarshals JSON messages.
280// Invalid JSON messages are logged and skipped.
281func QueueSubscribeJSON[T any](c MessagingClient, subject, queue string, handler func(T)) (Subscription, error) {
282 return c.QueueSubscribe(subject, queue, func(data []byte) {
283 var evt T
284 if err := json.Unmarshal(data, &evt); err != nil {
285 xlog.Warn("Failed to unmarshal NATS message", "subject", subject, "error", err)
286 return
287 }
288 handler(evt)
289 })
290}
291
292// RequestJSON sends a JSON request-reply via NATS, marshaling the request and
293// unmarshaling the reply. This eliminates the repeated marshal/request/unmarshal

Callers 1

StartMethod · 0.92

Calls 2

QueueSubscribeMethod · 0.65
handlerFunction · 0.50

Tested by

no test coverage detected