Skip to content

Latest commit

 

History

History
68 lines (49 loc) · 2 KB

README.md

File metadata and controls

68 lines (49 loc) · 2 KB

Chord Transit

Chord is a Clojure/ClojureScript abstraction layer that presents WebSocket connections as core.async channels.

Transit is a new data interchange format from Cognitect, similar to EDN. It benefits heavily from the performance of existing JSON message parsers, yet allows extensibility in the form of custom types.

Chord Transit adds Transit support to the Chord library.

Usage

Include the following in your project.clj:

	[jarohen/chord "0.4.2"]
	[lsnape/chord-transit "0.1.0"]

Clojure server

Initialise your message handler on the server:

(ns example.handler
  (:require [chord-transit.format]
            [chord.http-kit :refer [wrap-websocket-handler]]
            [clojure.core.async :refer [<! >! put! close! go-loop]]))

(defn my-ws-handler [{:keys [ws-channel] :as req}]
  (go
   (when-let [{:keys [message error] :as msg} (<! ws-channel)]
     (prn "Message received from client:" msg)
     (close! ws-channel)))

And then use the wrap-websocket-handler as follows:

(-> my-ws-handler
	(wrap-websocket-handler {:format :transit)

ClojureScript client

(:require [chord.client :refer [ws-ch]]
           chord-transit.format
           [cljs.core.async :refer [<! >! put! close!]])
 (:require-macros [cljs.core.async.macros :refer [go]])

 (go
   (let [{:keys [ws-channel]} (<! (ws-ch "ws://localhost:3000/ws" {:format :transit}))]
    (>! ws-channel "Hello server from client!")))

Caveats

  • Reading and writing of custom types is currently not supported.
  • Transit is still very new and still subject to change! See notices on the Cognitect site for latest information.

A Big Thanks