This page talks about the async and websocket extension for real-time application. Using the latest and greatest:
The API is still working in progress, but is fully working.
If interested, use GitHub issues page for suggestions, bug reports, or general discussions.
Please refer server.clj more detail, it has pretty clear docstr.
This API is not compatible with `2.0.0-rc4`.
Motivation
Why a new API
Simpler and more consistent than previous one
New feature: HTTP streaming
Unify the API for HTTP (streaming or long-polling) and WebSocket: allow gracefully degrading to long-polling/streaming when WebSocket is not available in client
Sever also get notified when client close/away for long-polling/streaming
Channel Protocol
Unified asynchronous channel interface for HTTP (streaming or long-polling) and WebSocket.
Channel defines the following contract:
open?: Returns true iff channel is open.
close: Closes the channel. Idempotent: returns true if the channel was actually closed, or false if it was already closed.
websocket?: Returns true iff channel is a WebSocket.
send!:
Sends data to client and returns true if the data was successfully sent,
or false if the channel is closed. Data is sent directly to the client,
NO RING MIDDLEWARE IS APPLIED.
Data form: {:headers _ :status _ :body _} or just body. Note that :headers
and :status will be stripped for WebSocket and for HTTP streaming responses
after the first.
on-receive:
Sets handler (fn [message-string]) for notification of client WebSocket
messages. Message ordering is guaranteed by server.
on-close:
Sets handler (fn [status]) for notification of channel being closed by the
server or client. Handler will be invoked at most once. Useful for clean-up
Get the channel
A macro with-channel is provided, example:
Long polling example
long polling is very much like streaming
Streaming example
Websocket example
A utility: ChannelStore
I find a utility like ChannelStore(or ChannelGroup) is quite helpful when using the API
If this is proven to be useful, http-kit can provide a built in more efficient implentation