Make WebSockets from ClojureScript

Now that we’ve created a WebSocket route on the server, we need to write the client-side portion of the socket. Once that’s done we’ll have full-duplex communication between the server and the client.

Create the WebSocket

Let’s start by creating a namespace called guestbook.ws for the WebSocket client. This namespace will be responsible for creating a socket as well as for sending and receiving messages over it. In the namespace declaration, let’s add a reference to cognitect.transit.

 (ns guestbook.ws
  (:require [cognitect.transit :as t]))

Next, let’s create an atom to house the channel for the socket and add helpers for reading and writing transit-encoded ...

Get Web Development with Clojure, 2nd 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.