sse icon indicating copy to clipboard operation
sse copied to clipboard

feat: Encode function can optimize byte array handling

Open KScaesar opened this issue 1 year ago • 0 comments

In scenarios where byte array JSON messages are forwarded (e.g., from MQ), the previous approach required unmarshalling into an object before encoding for SSE.

The updated logic allows []byte to be passed directly, simplifying message forwarding and reducing unnecessary conversions.

// before
byteData := messageFromMQ()
data := make(map[string]any)
json.Unmarshal(byteData, &data)
sse.Encode(new(bytes.Buffer), sse.Event{
	Data: data,
})

// after
byteData := messageFromMQ()
sse.Encode(new(bytes.Buffer), sse.Event{
	Data: byteData,
})

KScaesar avatar Sep 05 '24 12:09 KScaesar