ssestream

command
v0.4.0-beta.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2025 License: MIT Imports: 10 Imported by: 0

README

Server-Sent Events (SSE) Example

This example demonstrates how to use Server-Sent Events with Prefab to stream data from gRPC services to web clients.

Overview

Prefab's SSE support bridges gRPC streaming and web clients by automatically handling connection management, stream reading, and event formatting. You provide a function that calls your gRPC streaming method, and Prefab handles the rest.

Running the Example

Start the server:

go run examples/ssestream/main.go

Test with curl:

curl -N http://localhost:8080/counter

Or open client.html in a browser.

Usage

server := prefab.New(
    // Register your gRPC streaming service
    prefab.WithGRPCService(&CounterService_ServiceDesc, counterService),
    
    // Register SSE endpoint - Prefab handles everything
    prefab.WithSSEStream(
        "/counter",
        func(ctx context.Context, params map[string]string, cc grpc.ClientConnInterface) (CounterService_StreamClient, error) {
            client := NewCounterServiceClient(cc)
            return client.Stream(ctx, &CounterRequest{})
        },
    ),
)

That's it! Prefab automatically:

  • Creates and reuses gRPC client connections
  • Reads from the stream
  • Converts protobuf to JSON
  • Formats as SSE events
  • Handles context cancellation and cleanup

Client Usage

JavaScript
const eventSource = new EventSource('http://localhost:8080/counter');
eventSource.onmessage = (event) => {
    console.log('Received:', JSON.parse(event.data));
};
curl
curl -N http://localhost:8080/counter

Features

  • Path parameters: /notes/{id}/updates
  • Query parameters: params["query.paramName"]
  • Type-safe with Go generics
  • Automatic cleanup on client disconnect
  • Single shared connection for all SSE endpoints

See docs/reference.md for complete documentation.

Documentation

Overview

Package main demonstrates how to use Server-Sent Events (SSE) with Prefab and gRPC streaming.

Run the server:

go run examples/ssestream/main.go

Test with curl:

curl -N http://localhost:8080/counter

Or open client.html in a browser.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL