Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

327
Visualizações
How do I intercept outgoing tcp messages in node?

How can I write a simple stream which intercepts messages?

For example, say I want to log (or eventually transform) a message being sent over the wire by a user's socket.write(...) call.

Following is a minimal program which attempts to do this:

const net    = require('net');
const stream = require('stream');

const socket = new net.Socket();

const transformer = new stream.Transform({
  transform(chunk,e,cb){
    console.log("OUT:"+chunk.toString());
    cb();
  }});
//const client = socket.pipe(transformer); // <= prints "OUT:" on client, but nothing on server
const client = transformer.pipe(socket); // <= prints nothing on client, but "hello world" on server

socket.on('data', (data)=>{ console.log("IN:"+data.toString()); });
socket.connect(1234, 'localhost', ()=>{ client.write("hello world"); });

When I do socket.pipe(transformer), the client prints "OUT:" (like I want), but doesn't actually send anything to the server. When I swap the pipe locations, transformer.pipe(socket), nothing gets printed to the client but the message gets sent to the server.

Although not listed here, I also tried to use the Writable stream, which does print the message on the client, but it is never sent to the server (if I do a this.push(...) inside the Writable stream, it still doesn't seem to send to the server)

What am I missing here?

EDIT: Reformatted the code for clarity and updated the text

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

It looks like I needed to change the following line

socket.connect(1234, 'localhost', ()=>{ client.write("hello world"); });

to this

socket.connect(1234, 'localhost', ()=>{ transformer.write("hello world"); });

This is based on @Mr.Phoenix's comment. I expected .pipe() to return a new stream which I could use. I believe that is how Java's netty framework does it and I kept expecting node streams to work the same way.

over 4 years ago · Santiago Trujillo Relatório

0

You're not writing any data out of the stream.

You need to either this.push(chunk) or change the call to cb to cb(null, chunk).

See the docs about implementing transform streams for more info.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda