title: Postgres dynamically creating listeners
body: <p>I have a web site, backed by a postgres database. There are multiple webservers, that maintain persistent connections to each client, and needs to notify them of relevant events. The changes from one web-server may effect a user that is connected to another web-server, so I need some messaging layer between web-servers. Ideally I'd like to use the database for this, to keep things simple.</p>
  
  <p>So the two obvious ways, I see, would be:</p>
  
  <p>1) Have the webserver listen to <em>all</em> events, and web-server side filter the ones that are useful to it (e.g. relates to the users it currently has a connection with).</p>
  
  <p>OR:</p>
  
  <p>2) Dynamically create a listener for each client that connects, and remove the listener when the client disconnections.</p>
  
  <p>The first way means a <em>lot</em> of useless data going over the wire, but very easy for the webserver to filter (a simple hashmap lookup). The second way looks ideal <strong>if</strong> postgres is implemented in such a way that I can have thousands (to tens-of-thousands) of listeners created at any time. </p>
  
  <p>BTW I'm using the latest version of postgres</p>
  
  <p>Thoughts?</p>
  
  <p>Thanks very much!</p>