Hey guys,

 So for a while I have been working on a chat system that is written in Go 
and it targets itself to run efficiently on low resources systems like 
Raspberry Pi (under 512MB) and handle hundreds of thousands of connections 
easily (for really curious people URL is http://beta.raspchat.com and yes 
it's open-source so contributions are welcome). I have been deeply vested 
in PubSub part of the problem, where a user can subscribe to multiple chat 
rooms and when anyone sends a message to user its broadcasted to everyone 
in room. Over time with multiple iterations this is my solution so far. 


   - There is a global map of chat rooms (channels) containing map of 
   [name] -> RoomInfo. 
   - RoomInfo contains an array of channels (for users who have joined the 
   room) to send message back to users (call it *outbounds*), and a channel 
   (call it *inbound*) to receive messages from users.
   - Upon joining a room if it didn't existed the structure is initialized 
   properly and kept in dictionary against name of the channel. Everytime a 
   new room is created a new go routine is started to receive messages from 
   *inbound* channel to the room and then iterate sending copy of message 
   all *outbounds*.
   - Each new user creates a channel that can receive messages directed for 
   user (call it user_outbound), and when he/she joins a room, he/she keeps 
   reference to *inbound* and adds own channel to *outbounds* array.
   - Each user also spins of a goroutine listening to any message coming 
   from *user_outbound*, and processing it appropriately (Sends JSON over 
   websocket) and sending messages to given channel using the channel's 
   *inbound* reference we saved earlier.

 So far everything is like direct pipe and dictionaries (I used race 
condition free datatypes, but let's not get into that details) moving 
messages around using channels. But I was put off with my solution when I 
benchmarked it against nginx's nchan. I was consuming way more memory than 
what it was consuming and way more CPU than what I had implemented. Which 
made me go away from problem for a while and come back with a fresh mind. 
Now that I am looking at problem it made me wonder what is the most 
efficient way to implement just a pub/sub pattern among goroutines. I can 
see some libraries out there but reading code none seem to be far different 
than what I am thinking (or I might be looking at limited implementations). 
Any clues are greatly appreciated. 

Thanks,
Zohaib

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to