Skip to content

Instantly share code, notes, and snippets.

@hindol
Created June 12, 2020 18:06
Show Gist options
  • Save hindol/854ed24594e2909abfae7d45cd8cf391 to your computer and use it in GitHub Desktop.
Save hindol/854ed24594e2909abfae7d45cd8cf391 to your computer and use it in GitHub Desktop.
Access WebSocket session in Pedestal
(defn ws-listener
[_request _response ws-map]
(proxy [WebSocketAdapter] []
(onWebSocketConnect [^Session ws-session]
(proxy-super onWebSocketConnect ws-session)
(when-let [f (:on-connect ws-map)]
(f ws-session)))
(onWebSocketClose [status-code reason]
(when-let [f (:on-close ws-map)]
(f (.getSession this) status-code reason)))
(onWebSocketError [^Throwable e]
(when-let [f (:on-error ws-map)]
(f (.getSession this) e)))
(onWebSocketText [^String message]
(when-let [f (:on-text ws-map)]
(f (.getSession this) message)))
(onWebSocketBinary [^bytes payload offset length]
(when-let [f (:on-binary ws-map)]
(f (.getSession this) payload offset length)))))
;; in your service map:
::http/container-options {:context-configurator #(ws/add-ws-endpoints % ws-paths {:listener-fn ws-listener})}
@isaksky
Copy link

isaksky commented Jun 12, 2020

If you want reloading to work when using the REPL, use vars for your handlers:

(defn socket-on-text [session msg]
  (log/info :msg (str "A client sent - " msg :session session))
  (async/put! (get @ws-clients session) (str "Echo3:" msg)))

(def ws-paths
  {"/ws" {:on-connect (ws/start-ws-connection new-ws-client)
          :on-text (var socket-on-text)
          :on-binary (fn [session payload offset length] (log/info :msg "Binary Message!" :bytes payload))
          :on-error (fn [session t] (log/error :msg "WS Error happened" :exception t))
          :on-close (fn [session num-code reason-text]
                      (log/info :msg "WS Closed:" :reason reason-text))}})

In the example above, you can change socket-on-text, load it into your REPL, and the new function definition will handle new messages. For the ones baked into the map, they won't, and I couldn't see a better way to make that work (e.g., putting the var higher up in the chain, like we do for normal routes).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment