In this lesson, you will learn how to unpack a single multiplexed stream back into individual data channels so you can process them independently.
The Logic of Demultiplexing
Previously, we learned that multiplexing is the process of combining multiple streams into one to save on connection overhead (like sending multiple files over a single TCP socket). Demultiplexing is the exact inverse: it is the art of "unpackaging" that combined stream so that the original, distinct data packets reach their intended destinations.
Think of a multiplexed stream like a conveyor belt carrying mixed boxes labeled with "Source A," "Source B," and "Source C." A demultiplexer acts as a robotic sorter at the end of the line. It reads the metadata on each chunk to determine which destination pipeline should receive the data.
Implementation Pattern
To demultiplex, you typically use a Transform stream. As data arrives, you inspect a "header" or a "channel ID" prepended to each chunk. Based on that ID, you route the chunk to the correct destination:
const { Transform } = require('stream');
function createDemultiplexer(destinations) {
return new Transform({
objectMode: true,
transform(chunk, encoding, callback) {
// Assuming 'chunk' has a { channelId, data } structure
const { channelId, data } = chunk;
const target = destinations[channelId];
if (target) {
target.write(data);
}
callback();
}
});
}
In this setup, the demultiplexer isn't just passing data along; it acts as a traffic controller. By checking the channelId, it decides exactly which stream needs to process that specific piece of data.
Why This Matters for Backpressure
When demultiplexing, you must be careful: if one destination stream slows down (hitting its highWaterMark), it might cause the source multiplexed stream to stall. Even though you are separating the data, they share the same underlying connection. If you don't respect the backpressure of the individual downstream pipes, your demultiplexer will buffer too much data, potentially leading to memory leaks. Always ensure your demultiplexing logic propagates the drain signal correctly to the source.
Key Takeaway: Demultiplexing is the process of inspecting stream metadata to route incoming data chunks into the appropriate individual processing pipelines.