Class SseFormatter
java.lang.Object
org.a2aproject.sdk.server.util.sse.SseFormatter
Framework-agnostic utility for formatting A2A responses as Server-Sent Events (SSE).
Provides static methods to serialize A2A responses to JSON and format them as SSE events. This allows HTTP server frameworks (Vert.x, Jakarta/WildFly, etc.) to use their own reactive libraries for publisher mapping while sharing the serialization logic.
Example usage (Quarkus/Vert.x with Mutiny):
Flow.Publisher<A2AResponse<?>> responses = handler.onMessageSendStream(request, context);
AtomicLong eventId = new AtomicLong(0);
Multi<String> sseEvents = Multi.createFrom().publisher(responses)
.map(response -> SseFormatter.formatResponseAsSSE(response, eventId.getAndIncrement()));
sseEvents.subscribe().with(sseEvent -> httpResponse.write(Buffer.buffer(sseEvent)));
Example usage (Jakarta/WildFly with custom reactive library):
Flow.Publisher<String> jsonStrings = restHandler.getJsonPublisher();
AtomicLong eventId = new AtomicLong(0);
Flow.Publisher<String> sseEvents = mapPublisher(jsonStrings,
json -> SseFormatter.formatJsonAsSSE(json, eventId.getAndIncrement()));
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringformatJsonAsSSE(String jsonString, long eventId) Format a pre-serialized JSON string as an SSE event.static StringformatResponseAsSSE(org.a2aproject.sdk.jsonrpc.common.wrappers.A2AResponse<?> response, long eventId) Format an A2A response as an SSE event.
-
Method Details
-
formatResponseAsSSE
public static String formatResponseAsSSE(org.a2aproject.sdk.jsonrpc.common.wrappers.A2AResponse<?> response, long eventId) Format an A2A response as an SSE event.Serializes the response to JSON and formats as:
data: {"jsonrpc":"2.0","result":{...},"id":123} id: 0- Parameters:
response- the A2A response to formateventId- the SSE event ID- Returns:
- SSE-formatted string (ready to write to HTTP response)
-
formatJsonAsSSE
Format a pre-serialized JSON string as an SSE event.Wraps the JSON in SSE format as:
data: {"jsonrpc":"2.0","result":{...},"id":123} id: 0Use this when you already have JSON strings (e.g., from REST transport) and just need to add SSE formatting.
- Parameters:
jsonString- the JSON string to wrapeventId- the SSE event ID- Returns:
- SSE-formatted string (ready to write to HTTP response)
-