SpscReceiver<T> class final

Efficient receiver for SPSC channels.

Provides optimized message receiving for single-consumer scenarios. Designed for good performance and throughput.

Performance Optimizations

  • Efficient operations: Optimized communication path
  • Memory-friendly design: Minimizes unnecessary overheads
  • Single-subscription streams: No overhead from multi-consumer coordination

Usage Guidelines

  • Only ONE thread should use this receiver instance
  • Use tryRecv for polling in time-critical scenarios
  • Use recv for blocking receives when timing is flexible
  • Use stream for convenient async iteration (single-use only)
  • Monitor isDisconnected for end-of-stream detection

Example - High-frequency consumer:

final (tx, rx) = Spsc.channel<DataPoint>(capacity: 4096);

// Low-latency consumer loop
void consumeData() async {
  while (running) {
    switch (await rx.recv()) {
      case RecvOk(value: final data):
        // Process immediately - no queuing
        await processDataPoint(data);

      case RecvErrorDisconnected():
        print('Producer finished');
        return;

      case RecvErrorEmpty():
        // Impossible with recv() - only from tryRecv()
        break;
    }
  }
}

// Alternative: Stream-based consumption
await for (final data in rx.stream()) {
  await processDataPoint(data);
}
Available extensions

Properties

hashCode int
The hash code for this object.
no setterinherited
isDisconnected bool
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

close() → void
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
recv() Future<RecvResult<T>>
recvAll({Duration idle = Duration.zero, int? max}) Future<Iterable<T>>

Available on Receiver<T>, provided by the ReceiverDrainX extension

Receive multiple values with batching and idle timeout.
recvCancelable() → (Future<RecvResult<T>>, void Function())
recvTimeout(Duration d) Future<RecvResult<T>>

Available on Receiver<T>, provided by the ReceiverTimeoutX extension

Receive a value with a timeout to prevent indefinite waiting.
stream() Stream<T>
toBroadcastStream({bool waitForListeners = false, bool stopWhenNoListeners = true, bool closeReceiverOnDone = false, bool sync = false}) Stream<T>

Available on KeepAliveReceiver<T>, provided by the StreamReceiverX extension

Convert a channel receiver to a broadcast stream for multiple listeners.
toString() String
A string representation of this object.
inherited
tryRecv() RecvResult<T>
tryRecvAll({int? max}) Iterable<T>

Available on Receiver<T>, provided by the ReceiverDrainX extension

Drain all immediately available values without waiting.

Operators

operator ==(Object other) bool
The equality operator.
inherited