Skip to content

Commit

Permalink
realtime chat
Browse files Browse the repository at this point in the history
  • Loading branch information
vganshin committed Dec 28, 2023
1 parent befcfbd commit 4e75d23
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
15 changes: 8 additions & 7 deletions routes/ui/chat/:id_$send-message_post.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@
:author {:id (:id (auth/user-info)) :resourceType "User"}
:message (:message (box/form-params))})

[:<>
(turbo/send

(format "Chat/%s" (:id (box/route-params)))

[:turbo-stream {:action "append" :target "chat"}
[:turbo-stream {:action "prepend" :target "chat"}
[:template
[:div [:span {:style "font-weight: bold;"} (:id (auth/user-info)) ": "]
(:message (box/form-params))]]]
(:message (box/form-params))]]])

[:<>
[:turbo-stream {:action "replace" :target "chat-form"}
[:template
[:turbo-frame {:id "chat-form"}
[:form {:action "/ui/chat/$send-message" :method "post"}
[:form {:action (format "/ui/chat/%s/$send-message" (:id (box/route-params))) :method "post"}
[:input {:name "message"}]
[:button "Send"]]]]]

]
[:button "Send"]]]]]]
1 change: 1 addition & 0 deletions routes/ui/chat/:id_$subscribe_get.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(turbo/register-channel (str "Chat/" (:id (box/route-params))))
16 changes: 10 additions & 6 deletions routes/ui/chat/:id_get.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#_[:script {:src "https://cdn.tailwindcss.com"}]

[:script {:type "module" :src "https://unpkg.com/@hotwired/[email protected]/dist/turbo.es2017-esm.js"}]
[:script {:src "/ui/$dev-mode"}]
#_[:script {:src "/ui/$dev-mode"}]

[:div "User: " (:id (auth/user-info))
[:form {:method "POST" :action "/ui/auth/logout"
Expand All @@ -22,18 +22,22 @@

[:h1 "Chat"]

(let [messages (map :resource (box/sql ["select resource || jsonb_build_object('id', id) resource from chatmessage where resource#>>'{chat,id}' = ?" (:id (box/route-params))]))]
[:turbo-frame {:id "chat"}
(for [msg messages]
[:div [:span {:style "font-weight: bold;"} (:id (auth/user-info)) ": "]
(:message msg)])])
[:turbo-stream-source {:src (format "/ui/chat/%s/$subscribe" (:id (box/route-params)))}]

[:turbo-frame {:id "chat-form"}
[:form {:action (format "/ui/chat/%s/$send-message" (:id (box/route-params))) :method "post"}
#_[:input {:name "chat-id" :type "hidden" :value (:id (box/route-params))}]
[:input {:name "message"}]
[:button "Send"]]]

(let [messages (map :resource (box/sql ["select resource || jsonb_build_object('id', id) resource from chatmessage where resource#>>'{chat,id}' = ?" (:id (box/route-params))]))]
[:turbo-frame {:id "chat"}
(for [msg (reverse messages)]
[:div [:span {:style "font-weight: bold;"} (get-in msg [:author :id]) ": "]
(:message msg)])])



[:div
[:hr]
[:div {:style "font-family: monospace"} (format "Aidbox version %s" (box/version))]]
Expand Down

0 comments on commit 4e75d23

Please sign in to comment.