                 )
      (dosync (alter state-ref 
             (fn [state]
-               (assoc state :members (conj (:members state) {:path path :val val}))
-              ))))))
+               (assoc state :members (into #{} (conj (:members state) {:path path :val val})))
+              )))
+     (>!! member-event-ch {:left-members #{} :joined-members #{node-name}})
+     )))
      
        
 	    
+(defn- members-left [members u-members]
+  (clojure.set/difference (into #{} (map :path members)) (into #{} (map :path u-members))))
+
+(defn- members-joined [members u-members]
+  (clojure.set/difference (into #{} (map :path u-members)) (into #{} (map :path members))))
+
 (defn heart-beat [connector {:keys [members locks empherals] :as state}]
   "Sends out the heart beat and updates all TTL nodes,
    The members are queried and if any difference a fresh list created for the state
    which is returned"
   (send-updates connector (concat members locks empherals))
-  (let [u-members (get-members connector)]
-   
-    (if (not (= members u-members))
-     (merge state {:members u-members})
-      state)))
+    ;(prn "calling lef-members " members " " u-members)
+    (let [u-members (get-members connector)
+          left-members (if (> (count members) 0) (members-left members u-members) #{})
+          joined-members (members-joined members u-members)]
+        
+         ;publish event
+        (if (or (> (count left-members) 0) (> (count joined-members) 0))
+          (>!! (:member-event-ch connector) {:left-members left-members :joined-members joined-members}))
 
+        (if (not (= members u-members))
+		      (merge state {:members u-members})
+		           state)))
+
+(defn unregister-event-ch [{:keys [member-event-mult]} ch]
+  (untap member-event-mult ch))
+
+(defn register-member-event-ch [{:keys [member-event-mult]}]
+  "Returns a channel from which member register events can be read"
+  (let [ch (chan (sliding-buffer 10))]
+    (tap member-event-mult ch)
+    ch))
 
 (defn create-group-connector 
   ([host]

(ns group-redis.core
  (:require [fun-utils.core :refer [fixdelay]]
            [taoensso.carmine :as car :refer [wcar]])
  (:import [java.net InetAddress]))


  (clojure.string/join ["/" (clojure.string/join "/" (flatten [group-name "locks" path]))]))


(defn close [{:keys [state-ref member-event-ch heart-beat-ch]}]
  ;delete connection's state
  (close! heart-beat-ch)
  (close! member-event-ch)
  
  (dosync (alter state-ref (fn [state] {}))))
                        
(defn create-connection 
