B
B
brezhitskiy422020-10-15 12:50:21
Node.js
brezhitskiy42, 2020-10-15 12:50:21

How to save stream in Node.js?

How can a stream be stored in Node.js other than in memory? Tried using Redis but it only works with strings, dates and buffers. Current code example:

const Docker = require('dockerode');

const streams = {};

exports.startTerminal = async (id, socket) => {
  const container = docker.getContainer(id);

  try {
    const inspectData = await container.inspect();
    const isRunning = inspectData.State.Running;
    if (!isRunning) await container.start();

    const stream = await container.attach({ stream: true, stdin: true, stdout: true, stderr: true });

    socket.emit('terminal-started');

    stream.write('reset\n');
    stream.on('data', chunk => {
      socket.emit('terminal-output', chunk.toString('utf8'));
    });

    streams[id] = stream;
  } catch(err) {}
};

exports.terminalInput = (data, id, socket) => {
  const stream = streams[id];
  stream.write(data);
};


In this example, we receive user input from the client via sockets and write it to the terminal inside the docker container.

It turns out when the server is restarted, all saved streams will be lost.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Shumov, 2020-10-15
@inoise

Yes, because it's just a network connection, nothing more. If the container is lost, then in any case it must be recognized as dead

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question