     (stop-f :timeout 100)))
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Application
+(defonce messages-receive-chan (atom nil))
+(defonce messages-send-chan (atom (chan)))
+
+(defn start-message-loop [messages-receive-chan]
+  (go-loop []
+    (when-let [msg (<! messages-receive-chan)]
+      (println (str "message " msg))
+      (swap! message-log conj msg)
+      (message-dispatch msg @messages-send-chan)
+      (recur))))
+
+(go (while true
+      (let [msg (<! @messages-send-chan)]
+        (swap! message-send-log conj msg)
+        (chsk-send! (:id msg) (:message msg)))))
 
 (defn stop! []
   (do
+    (close! @messages-receive-chan)
     (stop-http-server!)
     (stop-router!)))
 
 (defn start! []
   (do
+    (reset! messages-receive-chan (chan))
+    (start-message-loop @messages-receive-chan)
     (start-http-server!)
-    (start-router!)))
+    (start-router! @messages-receive-chan)))
 
 (defn -main
   [& args]

            [clojure.core.async
             :as a
             :refer [>! <! >!! <!! go chan buffer close! thread
                     alts! alts!! timeout]]
            ))

(def memory-log (atom []))


  (def connected-uids                connected-uids) ; Watchable, read-only atom
  )

(defn event-msg-handler* [messages-receive-chan {:as ev-msg :keys [id ?data event ring-req]}]
  (>!! messages-receive-chan {:topic id :payload ?data :sender (:uid (:session ring-req))}))

(defonce router_ (atom nil))

(defn stop-router! []
  (when-let [stop-f @router_] (stop-f)))

(defn start-router! [messages-receive-chan]
  (stop-router!)
  (reset! router_ (sente/start-chsk-router! ch-chsk (partial event-msg-handler* messages-receive-chan))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Http routing
