Skip to content

Instantly share code, notes, and snippets.

@cgmartin
Last active December 21, 2015 11:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgmartin/6302379 to your computer and use it in GitHub Desktop.
Save cgmartin/6302379 to your computer and use it in GitHub Desktop.
Long running WAMP RPC call with progress status events
Long Run Button clicked
WAMP client initialized
WAMP connected 1377136104011-1
status:long-run RECEIVED 0
status:long-run RECEIVED 1
status:long-run RECEIVED 2
status:long-run RECEIVED 3
status:long-run RECEIVED 4
status:long-run RECEIVED 5
status:long-run RECEIVED 6
status:long-run RECEIVED 7
status:long-run RECEIVED 8
status:long-run RECEIVED 9
rpc:long-run RECEIVED true true
(ns clj-wamp-cljs.rpcstatus
(:require
[clj-wamp.client :as wamp :refer [wamp-handler]]
[clojure.browser.event :as event]))
(declare
add-btn-listener
trigger-wamp-call
on-wamp-open
on-wamp-close
on-wamp-event
call-long-run)
(def long-run-rpc-uri "http://myapp/rpc#long-run")
(def long-run-status-uri "http://myapp/status#long-run")
; On page load, init
(defn ^:export init []
(add-btn-listener))
(defn add-btn-listener []
; Button click triggers the websocket connection and rpc call.
; Only listen once so multiple clicks do not re-trigger call.
(let [long-run-btn (.getElementById js/document "long-run-btn")]
(event/listen-once long-run-btn "click" trigger-wamp-call)))
(defn trigger-wamp-call []
(.log js/console "Long Run Button clicked")
(if-let [ws (wamp-handler "ws://localhost:8080/ws"
{:on-open on-wamp-open
:on-close on-wamp-close
:on-event on-wamp-event})]
(.log js/console "WAMP client initialized")
(.log js/console "WAMP client init failed")))
(defn on-wamp-open [ws sess-id]
(.log js/console "WAMP connected" sess-id)
; Subscribe to status channel
(wamp/subscribe! ws long-run-status-uri)
; Issue the RPC request
(call-long-run ws))
(defn call-long-run [ws]
(wamp/rpc! ws long-run-rpc-uri ["test"]
(fn [ws success? result]
(.log js/console "rpc:long-run RECEIVED" success? (pr-str result))
; Stop websocket now that we have the result
(wamp/close! ws))))
(defn on-wamp-event [ws topic event]
; Listen for any pubsub status events
(cond
(= topic long-run-status-uri)
(.log js/console "status:long-run RECEIVED" (pr-str event))))
(defn on-wamp-close []
(.log js/console "WAMP disconnected")
(add-btn-listener))
2013-08-21 21:48:24,025 TRACE clj-wamp.server: Sending data: (0 1377136104011-1 1 clj-wamp/1.1.0-SNAPSHOT)
2013-08-21 21:48:24,026 INFO clj-wamp-cljs.websocket: WAMP client connected [ 1377136104011-1 ]
2013-08-21 21:48:24,031 TRACE clj-wamp.server: Data received: [5,"http://myapp/status#long-run"]
2013-08-21 21:48:24,036 TRACE clj-wamp.server: Data received: [2,"0.ywh61cuxx3g3z0k9","http://myapp/rpc#long-run","test"]
2013-08-21 21:48:24,037 INFO clj-wamp-cljs.websocket: WAMP call: 1377136104011-1 http://myapp/rpc#long-run 0.ywh61cuxx3g3z0k9 (test)
2013-08-21 21:48:24,038 INFO clj-wamp-cljs.websocket: Long Run RPC called test
2013-08-21 21:48:25,040 TRACE clj-wamp.server: Sending data: (8 http://myapp/status#long-run 0)
2013-08-21 21:48:26,045 TRACE clj-wamp.server: Sending data: (8 http://myapp/status#long-run 1)
2013-08-21 21:48:27,046 TRACE clj-wamp.server: Sending data: (8 http://myapp/status#long-run 2)
2013-08-21 21:48:28,047 TRACE clj-wamp.server: Sending data: (8 http://myapp/status#long-run 3)
2013-08-21 21:48:29,049 TRACE clj-wamp.server: Sending data: (8 http://myapp/status#long-run 4)
2013-08-21 21:48:30,050 TRACE clj-wamp.server: Sending data: (8 http://myapp/status#long-run 5)
2013-08-21 21:48:31,051 TRACE clj-wamp.server: Sending data: (8 http://myapp/status#long-run 6)
2013-08-21 21:48:32,052 TRACE clj-wamp.server: Sending data: (8 http://myapp/status#long-run 7)
2013-08-21 21:48:33,054 TRACE clj-wamp.server: Sending data: (8 http://myapp/status#long-run 8)
2013-08-21 21:48:34,054 TRACE clj-wamp.server: Sending data: (8 http://myapp/status#long-run 9)
2013-08-21 21:48:34,055 TRACE clj-wamp.server: Sending data: (3 0.ywh61cuxx3g3z0k9 true)
2013-08-21 21:48:34,062 INFO clj-wamp-cljs.websocket: WAMP client disconnected [ 1377136104011-1 ] :normal
(ns clj-wamp-example
(:require [org.httpkit.server :as http-kit]
[clj-wamp.server :as wamp]
[clojure.tools.logging :as log]))
(def long-run-rpc-uri "http://myapp/rpc#long-run")
(def long-run-status-uri "http://myapp/status#long-run")
(defn long-run [data]
(log/info "Long Run RPC called" data)
(doseq [i (range 10)]
(Thread/sleep 1000)
; Only send status to the client who called
(wamp/emit-event! long-run-status-uri i [wamp/*call-sess-id*]))
{:result true})
(defn my-wamp-handler
"Returns a http-kit websocket handler with wamp subprotocol"
[request]
(http-kit/with-channel request channel
(wamp/http-kit-handler channel
; Expose RPC and PubSub topics
{:on-call {long-run-rpc-uri long-run-rpc}}
:on-subscribe {long-run-status-uri true}
:on-publish {long-run-status-uri true}})))
(http-kit/run-server my-wamp-handler {:port 8080})
@cgmartin
Copy link
Author

A simple WAMP server with a long running RPC call long-run.
The call will simulate progress status by looping/sleeping and sending a status event to a PubSub channel. When done looping the result is returned.

In the client, a button click will start a WAMP websocket connection. Immediately upon open it will subscribe to the status PubSub channel and issue the long running RPC call. A PubSub event listener on-wamp-event receives all of the progress status events.

On completion of the RPC call, the client closes the WAMP connection and the button listener is set again.

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