   (let [last-id (@clients-last-id channel) cur-id (+ @max-id 1)]
     (do
       (println packet @clients-last-id)
-      (if (not-nil? last-id) (do
-        (send! channel (json-str {:status "ok" :data (filter (fn [msg] (>= (:id msg) last-id)) @all-msgs)}))
-        (swap! clients-last-id assoc channel cur-id)
-        (clear-old-msgs))))))
+      (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))}))
+          (swap! clients-last-id assoc channel cur-id)
+          (clear-old-msgs))))))
 ; perfomance dump version 1
 (defmethod process-packet ["dump" "1"] [packet channel] (dump))
 ; default method - simple print packet

; login version 1
(defmethod process-packet ["login" "1"] [packet channel]
  (do
    (let [login (:data packet)]
      (swap! clients         assoc channel login)
      (swap! clients-last-id assoc channel (- max-id 20)
      (println packet @clients-last-id)
      (send! channel (json-str {:status "ok"})))))
; logout version 1
(defn logout [channel]
  (do
    (swap! clients dissoc channel)
    (swap! clients-last-id dissoc channel)
    (println @clients-last-id)

      (.write wrtr line)))
    (reset! log-msgs [])))
; messages
(defonce all-msgs (ref [{:id (next-id) :time (now) :msg "Добро пожаловать в чат!" :author "system"}]))
(defn add-msg-to-storage [msg]
  (dosync (let [all-msgs* (conj @all-msgs msg)] (ref-set all-msgs all-msgs*))))
(defn min-msg-id []
