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

300
Visualizações
How do i make my server run and receive messages in real time with NodeJS?

I can create a NodeJS Server, but it looks like it is not receiving messages in real time. I have a device that will send messages to this server im creating over the time, and i just want to ''console.log'' the response every time it receives it. It is that simple. That said, is important to notice that I DO NOT have control over the server sending me the data. I haven't created it and i can't touch it. It is external. But i do know that it's sending the data to my server, i just don't know how to receive it in real time....

My code right now is this one. Feel free to change it and completely rework it if you want, im not very experienced in NodeJS. Also feel free to use any other library, as i have tried doing so using many others with no success.

import http from 'http';

const host = 'MY.IP';
const port = 0000;

const requestListener = function (req, res) {
  res.writeHead(200);
  res.end("--SERVER ONLINE.");
};

const server = http.createServer(requestListener);
server.listen(port, host, () => {
    console.log(`Server is running on http://${host}:${port}`);
});

EDIT: I believe i have failed to explain properly: What I need is to PRINT the data im receiving from the OTHER SERVER in the one from the code above. Code above creates server A. server A is receiving data from server B. I need to print the data server B sent on server A. Server B is sending the data via HTTP POST request.

EDIT2: I have changed my code, and im getting close to doing what i need,but still, no sucess. Now i have:

import http from 'http';
const PORT = process.env.PORT || 8000;
const httpServer = http.createServer();


httpServer.on('request', (request, response) => {
    if (request.url == '/')
        response.end('Server online!');
        console.log(request);
});

httpServer.listen(PORT, () => console.log(`Server running at ${PORT}`));

Everytime there is a request for the server it does indeed, print the request on my screen. I can see that server B is sending the data to this server i created, working fine and the data is 100% being sent. I just need to "console.log" the data im receiving now. Still no success.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to listen for requests sent to your server by using <httpserver>.on('request', callback)

Try something like this

import http from 'http';
const PORT = process.env.PORT || 3000;
const httpServer = http.createServer();


httpServer.on('request', (request, response) => {
    if (request.url == '/')
        response.end('reached server!');
});

httpServer.listen(PORT, () => console.log(`running on port ${PORT}`));

In this example, by going to http://localhost:3000/ it should show "reached server!"

about 4 years ago · Juan Pablo Isaza Relatório

0

I have found the solution by myself. Took me almost 2 days, as i have no prior experience in NodeJS, but here is the answer. I hope it helps someone in the future:

import http from 'http';
const PORT = process.env.PORT || 8000;
const httpServer = http.createServer();


httpServer.on('request', (request, response) => {
    if (request.url == '/')
        response.end('Server online!');
request.on('data',function(data){
         console.log(data.toString());
});
      });



httpServer.listen(PORT, () => console.log(`Server running at: ${PORT}`));

The part i was missing was this one:

request.on('data',function(data){
             console.log(data.toString());
    });

I didn't knew i could use a 'on' event on a 'request',because 'request' is already an event by itself. But i could, and this solves my problem.

Now server A is receiving and printing every message from server B in real time, and i can do whatever i want with the response!

about 4 years ago · Juan Pablo Isaza 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