Skip to content

@epikodelabs/streamix


@epikodelabs/streamix / src/public-api / correlate

Function: correlate() ​

correlate<Q, A, K>(responses, options): Operator<Q, Correlated<Q, A>>

Defined in: projects/libraries/streamix/src/lib/operators/correlate.ts:67

Joins outgoing requests with incoming responses using a correlation key.

Use this for request/response protocols carried over separate streams — RPC over a socket or message bus, for example — where responses can arrive out of order. Each request is held until its matching response arrives, and the output emits { request, response } pairs.

Responses that arrive before their request are retained by key (the latest response per key wins) and matched when that request appears. A key may have only one outstanding request at a time: emitting another request with the same key before the first is matched fails the output.

The output completes when the request stream ends and every pending request has been answered. If the response stream ends while requests are still pending, or a request exceeds the optional timeout, the output fails.

Typical scenarios:

  • RPC over a shared socket, worker, or message bus where responses arrive out of order.
  • Matching commands to acknowledgements by request id.
  • Accepting responses that arrive before their request.

Type Parameters ​

Q ​

Q

The request value type.

A ​

A

The response value type.

K ​

K

The correlation key type.

Parameters ​

responses ​

PipeInput<A>

Stream, atom, iterable, or promise of incoming responses.

options ​

CorrelateOptions<Q, A, K>

Key extractors and the optional request timeout.

Returns ​

Operator<Q, Correlated<Q, A>>

An operator that emits matched request/response pairs.

Example ​

ts
// `outgoing` is the request stream; `incoming` is the response stream.
const pairs = pipe(outgoing, correlate(incoming, {
  requestKey: request => request.id,
  responseKey: response => response.id,
  timeout: 5_000,
}));

Released under the MIT License.