On Mar 1, 2014, at 2:01 PM, Ben Hyde <bhyde@pobox.com> wrote:

On Mar 1, 2014, at 3:04 PM, Ron Garret <ron@flownet.com> wrote:
server-side events

I assume, you cobbled together your own Hunchentoot compatible implementation?  Or, is there one kicking about I've not noticed?  - ben

Here’s a self-contained SSE demo:


(require :hunchentoot)

(rename-package :hunchentoot :hunchentoot '(:tbnl :ht))

(defvar $server (make-instance 'ht::easy-acceptor :port 1234 :access-log-destination nil))

(ht:start $server)

(ht:define-easy-handler (sse-test :uri "/sse-test") ()
  "<script>
var source=new EventSource('sse-stream');
source.onmessage=function(event) {
document.getElementById('result').innerHTML=event.data + '<br>';
};
</script>
Server-side event demo<br>
<div id=result></div>
")

(defun set-header (name value) (setf (ht:header-out name) value))

(ht:define-easy-handler (sse-stream :uri "/sse-stream") ()
  (set-header "content-type" "text/event-stream")
  (set-header "cache-control" "no-cache")
  (let ((stream (ht:send-headers)))
    (dotimes (i 10)
      (write-sequence (encode-string-to-octets
                       (format nil "data: test ~A~A~A" i #\newline #\newline))
                      stream)
      (force-output stream)
      (sleep 0.5))))