Search code examples
javascriptnode.jssocketstcp

Where does all data send with nodejs method socket.write?


Good day! Where does all data send when nodejs method socket.write is called? I understand that socket runs of the server side for each client. But where exactly does data go? to client? On official nodejs documentanion there is no info about destination. Thank you for response.


Solution

  • You cannot successfully write to a socket unless you (or some part of your nodejs software) first connects it to some other socket somewhere.

    A socket server listens for connection requests, and then accepts them as they arrive. (When you use node express to make a web server, express handles this for you.) A client connects to a socket server. Once the pair of sockets are connected, data you write into one of the sockets causes a data event on the other one.

    The two sockets may be on different machines in different locations. That's the miracle of global networking.

    So where does data you write go? To the other socket in the pair.

    If you are using datagrams (not connections) it's slightly different. The data you write contains the destination address. But you probably are not using databgrams. If you are, you are probably using a protocol stack like RTSP or UDP instead of TCP.