'Slacker 0.6: Exposing multiple namespaces'

Fri 03 February 2012
  • ANN tags:
  • clojure
  • project
  • slacker published: true comments: true

After 98 commits in about one month, I'm glad to announce [slacker "0.6.1"].

One thing in slacker 0.6.x is you can expose multiple namespaces from a single server.

Suppose you have two namespaces `redday.stats` and `redday.api`, both contains functions you want to expose.

[cc lang="clojure"]
(start-slacker-server [(the-ns 'redday.stats)
(the-ns 'redday.api)]
6565)
[/cc]

This will expose `redday.stats` and `redday.api` on port 6565.

On the client side, we have a new `use-remote` behaviors like clojure's use. Instead of local one, it imports functions from a remote namespace to your current namespace.

[cc lang="clojure"]
(use 'slacker.client)
;; create a slacker client
(def scp (slackerc "127.0.0.1:6565"))

(use-remote 'scp 'redday.api) ;; caution, use the symbol of 'scp here
(use-remote 'scp 'redday.stats)

;;top-titles is a function in redday.api
;;now you can use the remote function transparently
(top-titles "programming")

;;check function metadata you can find more slacker properties
(meta top-titles)
[/cc]

If you need to configure callback to a particular function, you can still use `defn-remote` to specify the callback function. In slacker 0.6.0, a `:remote-ns` is required when you define such a remote function.
[cc lang="clojure"]
(defn-remote top-titles :remote-ns "redday.api" :callback #(println %))
[/cc]

The complete code example (both server and client) can be found here.

In next post, I will explain another big new feature of 0.6.x, cluster support.