Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
;; This example was created to help the author of this thread:
;; Before loading this code, do this in the REPL:
;; (ql:quickload '(log4cl spinneret clack lack))
(defpackage #:server-side
(:use #:cl))
(in-package server-side)
(defun render-page ()
(:script "
var source = new EventSource('/events');
source.addEventListener('message', function(e) {
var events = document.getElementById('events')
var new_element = document.createElement('li')
new_element.innerText =;
}, false);
(:h1 "Server-side events Clack example")
(:h2 "Events:")
(:ul :id "events")))))
(defun main-app (env)
(declare (ignorable env))
(list 200 '(:content-type "text/html")
(list (render-page))))
(defun events-handler (env)
(declare (ignorable env))
(let ((output (getf env :raw-body)))
(lambda (handle-normal-response)
(declare (ignorable handle-normal-response))
(format output "HTTP/1.1~C~C" #\Return #\Newline)
(format output "Content-Type: text/event-stream~C~C" #\Return #\Newline)
(format output "~C~C" #\Return #\Newline)
(format output "~C~C" #\Return #\Newline)
(force-output output)
(loop for i from 1 upto 1000
do (log:info "Returning event" i)
(format output "data: Event ~A~%~%" i)
(force-output output)
(sleep 1))
(sb-int:broken-pipe ()
;; This condition will be signaled if user will close the "tab"
(log:info "Connection was closed")
(defun make-app ()
(:mount "/events"
(defvar *server* nil)
(defun start-server (&key (port 8080))
(when *server*
(error "Server already started"))
(setf *server*
(clack:clackup (make-app)
:port port)))
(defun stop-server ()
(when *server*
(clack:stop *server*)
(setf *server*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.