     (let [author (@clients channel) text (strip-html-tags (:data packet))]
       (do
         (dosync (ref-set all-msgs (conj @all-msgs {:id (next-id) :time (now) :msg text :author author})))
-        (send! channel (json-str {:status "ok"}))))))
+        (send! channel (json-str {:status "ok"}))
+        (clear-old-msgs)))))
 ; fetch version 1
-(def not-nil? (complement nil?))
 (defmethod process-packet ["fetch" "1"] [packet channel]
   (let [last-id (@clients-last-id channel) cur-id (+ @max-id 1)]
     (do
-      (println packet @clients-last-id)
       (if (not-nil? last-id)
         (do
-          ;(println (reverse (filter (fn [msg] (>= (:id msg) last-id)) @all-msgs)))
-          ;(println (filter (fn [msg] (>= (:id msg) last-id)) @all-msgs))
-          (send! channel (json-str {:status "ok" :data (sort-by :id < (filter (fn [msg] (>= (:id msg) last-id)) @all-msgs))}))
+          (send! channel
+            (json-str {:status "ok" :data (sort-by :id < (filter (fn [msg] (>= (:id msg) last-id)) @all-msgs))}))
           (swap! clients-last-id assoc channel cur-id)
           (clear-old-msgs))))))
 ; perfomance dump version 1

    (let [login (:data packet) id (- @max-id 20)]
      (swap! clients         assoc channel login)
      (swap! clients-last-id assoc channel id)
      (println packet @clients-last-id)
      (send! channel (json-str {:status "ok"}))
      (dosync (ref-set all-msgs (conj @all-msgs {:id (next-id) :time (now) :msg (format "Пришел: %s" (@clients channel)) :author "system"}))))))
; logout version 1
(defn logout [channel]
  (do
    (dosync (ref-set all-msgs (conj @all-msgs {:id (next-id) :time (now) :msg (format "Ушел: %s" (@clients channel)) :author "system"})))
    (swap! clients dissoc channel)
    (swap! clients-last-id dissoc channel)
    (println @clients-last-id)
    (clear-old-msgs)))
(defmethod process-packet ["logout" "1"] [packet channel]
  (do

  (apply min (map (fn [msg] (:id msg)) @all-msgs)))
(defn clear-old-msgs []
  (let [min-id (min-clients-last-id)]
    (dosync
      (ref-set all-msgs (filter (fn [msg] (>= (:id msg) (- min-id 10))) @all-msgs))
      (let [total (count @all-msgs)]
        (if (> total 4096)
          (ref-set all-msgs (vec (drop (- total 4096) @all-msgs)))
          (ref-set all-msgs @all-msgs))))))
(def not-nil? (complement nil?))
; multi method, dispatch by command and version
(defn dispatcher [packet channel] [(:command packet) (:version packet)])
(defmulti  process-packet dispatcher)
