Broadcast Message to all listeners.
gravypod opened this issue · 3 comments
This is more of a feature request to see if such a thing is possible.
I was thinking of writing an IRC server or some such system with libmill and I was looking through the docs. It doesn't seem like this sort of system is supported without managing an array of channels (one for each listener).
This feature was also brought up in a mailing list that I found when googling: http://www.freelists.org/post/libmill/broadcast-effect
And the answer that was proposed seemed more like an anti-pattern in this case: http://www.freelists.org/post/libmill/broadcast-effect,1
Is it possible to support
`coroutine void on_c(chan channel)
{
choose {
in(ch, char*, message):
printf("%s\n", message);
end
}
}
void main()
{
char *message = "Hello World";
chan ch = chmake(int, 0);
go(on_c(ch));
go(on_c(ch));
go(on_c(ch));
chbroadcast(ch, message);
}`
Where the output is:
Hello World
Hello World
Hello World
This would, in my opinion, create some interesting design patterns for message/event driven systems in C.
I would say you can create a broadcast channel using existing primitives. What you need is a coroutine that reads message from in channel, writes it to N out channels. Repeats.
@sustrik how would one handle forwarding any message type to anything capable of receiving it? That's something that, to my understanding, must be done above the level of abstraction provided by choose. You'd also need a way to clean up references to dead channels and I'm not sure of a way to do that.
Well, you are in Golang area here. libmill tries to map 1:1 to Golang, so the standard answer is "do it as you would in Golang".
If you are interested in going beyond golang have a look at http://libdill.org