Upgrading to Sente

Now that we’ve done a scratch implementation of WebSockets, let’s look at a popular Clojure(Script) WebSockets library: Sente.[62]

Sente is a little more complicated than our toy implementation, but it brings a lot of great features in exchange.

Ajax Fallback Support

Sente automatically switches to Ajax polling if WebSockets aren’t available.

Keep-Alives

Sends messages periodically to prevent connections from dropping and to kill stale connections.

Message Buffering

Leverages core.async to buffer messages for us.

Encoding

Serializes and de-serializes data for us.

Security

Supports Ring anti-forgery middleware.

Let’s add the dependency to our project.clj and get started:

 [com.taoensso/sente ...

Get Web Development with Clojure, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.