Olli
Olli

Reputation: 192

Redirect linux process stdout to multiple incoming tcp connections

I have a worker process than produces its output onto the stdout pipe.

I would now want to have a service that echoes the stdout of this process to multiple incoming tcp peer connections.

In other words, I want to

The stdin pipe can be ignored.

Question accordingly: Can I achieve this with some existing command line utility, or do I need to write a repeater tool myself e.g. with Python?


I tested using socat and that can serve part of the need, but not in full:

For reference, this is how I tested using socat:

An oneliner bash dummy example that serves updating round N echo entry to port 8080:

for i in {0..10000}; do echo "round ${i}"; sleep 1; done | socat - TCP-LISTEN:8080,fork,reuseaddr

If listening this example dummy stream with two nc localhost 8080 peer connections, both peers receive some of the stdout stream but neither receive all:

Connection 1 receives:
round 1
round 2
round 4
round 7

Connection 2 receives:
round 3
round 5
round 6
round 8

Expectation is that both connections would receive all the 'round 1, round 2, round 3 ...' etc entries.

Upvotes: 3

Views: 68

Answers (2)

dest-unreach
dest-unreach

Reputation: 461

Socat 1.8 distribution contains the script socat-mux.sh for use on Linux that should do what you need:

for i in {0..10000}; do
  echo "round ${i}"
  sleep 1
done | socat-mux.sh TCP-LISTEN:8080 STDIN

Now connect with TCP clients, they should receive the data.

Please note that this script uses broadcasts on loopback, so any local users might read along.

Upvotes: 5

Thor
Thor

Reputation: 47189

Sounds like you are looking for a pub-sub solution. There are many ways to achieve this, e.g.:

Redis

Producer(s):

for i in {0..10000}; do echo "round ${i}"; sleep 1; done |
while read s; do printf "publish topic '%s'\n" "$s" | redis-cli; done

Subscriber(s):

printf "subscribe topic\n" | redis-cli

MQTT

Producer(s):

for i in {0..10000}; do echo "round ${i}"; sleep 1; done |
mosquitto_pub -t topic -l

Subscriber(s):

mosquitto_sub -t topic

ZeroMQ

ZeroMQ can be configured in a myriad of ways, e.g. you can find an example implementation of the publish-subscribe pattern here.

Upvotes: 1

Related Questions