To build real-time, bidirectional streaming services, implement the HandleStream method in your service implementation. This method serves as the entry point for all WebSocket communication. You use the generated Stream interface to interact with the connection.
Key capabilities of the Stream interface:
Recv(ctx): Receives and dispatches incoming JSON-RPC requests to the appropriate service method.SendMethodName(...): Sends responses or notifications for a specific method.SendError(...): Sends JSON-RPC error responses.Close(): Closes the connection.
Note that service methods are called automatically when Recv() processes a matching request, but you can also call them manually from HandleStream for server-initiated communication.
func (s *serviceImpl) HandleStream(ctx context.Context, stream ServiceName.Stream) error {
// User implements their streaming strategy here.
// Can listen to channels, timers, events, etc.
// Can call stream.Recv() to process incoming JSON-RPC requests.
// Can call stream.SendMethodName() to send responses or notifications.
}