clojurewebsocketaleph

How to ensure websocket connection is kept alive in Aleph


I'm trying to interface with Slack's RTM API with Aleph.

Currently I have this code:

(defn connect-socket []
  (let [conn @(http/websocket-client (get-websocket-url))]
    (s/consume #(prn %) conn)
    (send-message conn {:type "ping"}) ;; just to check if send-message works
    (s/on-closed conn (prn "closed"))))

It works well for the first few times, and then conn stops receiving messages from Slack after some inactivity. Neither does it print "closed", which seems to signify that the stream isn't closed. I'm not quite sure what is happening here.

How can I keep-alive the websocket connection, or auto-reconnect if disconnected? I've seen some code in the wild doing pings, but I don't think I understand the code well enough to adapt it.


Solution

  • I think that you have to send ping messages because :

    Therefore you have to do some "pings" regularly. Regarding the code, on-closed wants a function as second argument, so : (s/on-closed! conn #(prn "closed"))