0

I want to build a TCP server (say, tcpserver.js) created with net.createServer(). I have a TCP client (say, eventSource.js) that writes to this server at a high frequency e.g. 100s per sec. I have a TCP client eventSink.js that will connect to this TCP server and process the received data. This TCP client will connect only once every few hours and for only few minutes. Here are my questions.

  1. How do I handle back-pressure during the time when the eventSink.js is not connected?
  2. What does this statement from NodeJs Net documentation mean? How does a 'no listener' look like in the socket.on('data', ...) look like?

    The data will be lost if there is no listener when a Socket emits a 'data' event.

  3. How do I differentiate the client connections from eventSource.js and eventSink.js ?

Can you please show me some pseudo-code to get started?

This is what I have so far for tcpserver.js.

const net = require('net')

class TcpServer {
  constructor(port) {
    this.tcpServer = net.createServer((c) => {

      c.on('close', (err) => {
        if (err) {
          console.log('Error in closing socket connection.')
        } else {
          this.tcpServer.close()
        }
      })

      c.on('data', (buffer) => {
        // help...
      })


    this.tcpServer.listen(port, () => {
      // ...
    });

  }
}

exports.TcpServer = TcpServer
cogitoergosum
  • 2,309
  • 4
  • 38
  • 62
  • 1
    What do you intend to do with the data that arrives from `eventSource.js` when `eventSink.js` is not connected? Throw it away? Store it somewhere so `eventSink.js` can pick it up later? What? This seems like a pretty basic architectural question that you haven't made any mention of and it's at the crux of what you would do in the `data` event. – jfriend00 May 08 '20 at 04:05
  • The data from `eventSource.js`, when `eventSink.js` is not connected, will be thrown away. – cogitoergosum May 08 '20 at 04:07
  • As for distinguishing between the two types of connections, you need some sort of protocol that allows the connecting client to tell the server what it wants to do. Since you're using plain TCP, this is up to you to design your own protocol or find a higher level protocol that matches your needs and use it. – jfriend00 May 08 '20 at 04:07
  • 2
    If you're going to throw the data away there is no backpresssure. Unclear what you're asking, or why on earth you would do this. – user207421 May 08 '20 at 04:47
  • There are _clear_ reasons on earth why I am doing this....just can't reveal here. :) I was under the impression that, regardless of 'read' clients, a continuous 'write' to the `TcpServer` will overwhelm the TCP server. – cogitoergosum May 08 '20 at 05:03
  • @cogitoergosum - TCP has it's own flow control. If the receiving TCP end of things is getting overwhelmed at the network level, TCP all by itself will tell the client to slow down. See this article: https://www.brianstorti.com/tcp-flow-control/. This is done at the network level. – jfriend00 May 08 '20 at 05:19
  • Thanks for this link. What is `await drain(writable)` when `writable.write(chunk) === false` then? Does `false` happen when 'slowing down' starts hitting high water mark? https://nodejs.org/api/stream.html#stream_piping_to_writable_streams_from_async_iterators – cogitoergosum May 08 '20 at 05:24

0 Answers0