pedestal

jdkealy 2020-05-24T20:22:40.131500Z

How are you supposed to get the current user in a websocket connection in pedestal/jetty? Can’t seem to find any reference to the request at all in the connection request

...
::http/container-options {:context-configurator #(ws/add-ws-endpoints % ws-paths)}
... 

(def ws-paths
  {"/ws" {:on-connect (fn [e]
                        (let [[sess channel] ((ws/start-ws-connection new-ws-client) e)]
                          (println "CANT FIND A TOKEN OR ANYTHING HERE" e)
                    channel
                    ))
          :on-text (fn [msg] (log/info :msg (str "A client sent - " msg)))
          :on-binary (fn [payload offset length] (log/info :msg "Binary Message!" :bytes payload))
          :on-error (fn [t] (log/error :msg "WS Error happened" :exception t))
          :on-close (fn [num-code reason-text]
                      (log/info :msg "WS Closed:" :reason reason-text))}})

adamfeldman 2020-05-25T14:58:33.142400Z

There might be something to reference in this code: https://github.com/ptaoussanis/sente

2020-05-25T21:08:54.142800Z

@jdkealy You can access to the upgrade request via the websocket session object. Check out the Jetty websocket api for details.

jdkealy 2020-05-24T20:25:47.132400Z

Also, I’m trying to make the connection with some query parameters. I want to A:) make the websocket connection for the associated topic and B:) authenticate that the user has access to the topic

2020-05-24T21:05:26.133Z

this came up before, i haven't tried it but the notes are here https://clojurians-log.clojureverse.org/pedestal/2020-04-09

jdkealy 2020-05-24T21:32:42.133600Z

thanks, it’s a bit over my head lol

jdkealy 2020-05-24T21:34:35.134400Z

is the hindol.adhya suggesting to replace #(ws/add-ws-endpoints % ws-paths) with ws-listener ? I’ll give that a try.

hindol 2020-05-24T21:50:42.135100Z

That's my snippet. The last line shows how to use it.

1
hindol 2020-05-24T21:53:04.137300Z

Ben has an alternative approach below that. But in both cases, you supply your own listener function to add-ws-endpoints.

jdkealy 2020-05-24T22:05:27.139Z

ah cool i see… So you need to dip a bit into java. Between (.getHeaders _request) / (.getRequestURI _request) I could hack getting the current user / request params. It would be super convenient to be able to leverage the interceptors, but this gets me what I need. Many thanks!