0

Using bash we might have something like this with netcat:

nc -lk -U /my/fifo | while read line; do
   # line => {"client_id":123}
done;

clients send data like this:

echo '{"client_id":123,"data":"foo"}' | nc -U /my/fifo

and clients wait for data like this:

echo '{"client_id":123}' | nc -U /my/fifo | while read line; do

done;

my question - how can I send data from one client to another client, without involving all the clients? On the "server", I would need to store a reference to each client, so when I get a message from one client, I know who to forward the message to? How can I store a reference to the clients that are currently connected? Just like websockets, I want to create channels, where anyone registered to that channel will get the message.

  • and as an aside, if a netcat client sends a message to the netcat server, I am not sure how to respond directly (write back to that one client). – Alexander Mills Jun 5 at 1:12
  • 1
    Just commenting: There is an uneven number of double quotes in the text strings that you are giving to nc. – Kusalananda Jun 5 at 6:38
  • Thanks, quotes fixed as far as the OP, more of a conceptual question here – Alexander Mills Jun 5 at 6:41
-1

So here is the thing, I guess I can respond to clients with:

echo "this is the response" | nc -lk -U /my/fifo

but the whole point is how do I respond differently depending on who the client is? I mean how can you create something very useful if there is no logic based on the client request etc?

My only guess is to have two servers:

nc -lk -U /my/fifo1 | while read line; do

  # custom logic goes here

done > nc -lk -U /my/fifo2

so then clients connect with:

echo '{"client_id":123}' | nc -U /my/fifo1 

nc -U /my/fifo2 | while read line; do

done; 

this betrays what I know about how tcp and unix domain sockets work with Node.js etc, but I guess with bash we have to do it this way?

I also think to avoid race conditions might have to put the above two commands in a single pipe somehow.

  • This does not look like an answer. This should probably be an edit to your question. – Steffen Ullrich Jun 5 at 3:52
  • partial answer, I don't think the question needs to be longer – Alexander Mills Jun 5 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.