Reblocks extension allowing to add a bidirectional communication via Websocket between a backend and Reblocks widgets.
reblocks-websocket - Reblocks extension adding a bidirectional communication via Websocket.
REBLOCKS-WEBSOCKET ASDF System Details
Description: Reblocks extension allowing to add a bidirectional communication via Websocket between a backend and Reblocks widgets.
Author: Alexander Artemenko email@example.com
Bug tracker: https://github.com/40ants/reblocks-websocket/issues
Source control: GIT
This module allows you to push some information from backend to frontend and. In this case, updates of widgets's state on the client are initiated by server. For example, you can have some sort of long running process on the server and need to show it's status to the user.
This library depends on Reblocks (Weblocks fork) and a websocket-driver. If you will use Woo server then probably you'll need this fork of the websocket-driver (make-woo-work-from-separate-threads branch). However, may be the recent version of websocket-driver will work just find, I don't know.
You can install this library from Quicklisp, but you want to receive updates quickly, then install it from Ultralisp.org:
(ql-dist:install-dist "http://dist.ultralisp.org/" :prompt nil) (ql:quickload :reblocks-websocket)
Define you widget and inherit it from the
(reblocks:defwidget counter-box (reblocks-websocket:websocket-widget) ((counter :initform 0 :accessor counter)))
Define a code which will start some sort of background activity. In this example we are doing it right when widget was created in the beginning of the user session, but of cause, you can do it as a reaction on an action.
(defmethod initialize-instance ((instance counter-box) &rest restargs) (declare (ignorable restargs)) (call-next-method) (reblocks-websocket:in-thread ("Update counter") (sleep 3) ;; Updating counter (incf (counter instance)) (reblocks:update instance)))
That is it. Define a render method as usual and use the widget on the page. Counter will be updated automatically. This works like a magic, a framework makes all dirty work under the hood.
reblocks-websocket:send-command method-name &rest args
JS script to frontend via Websocket.
reblocks-websocket:in-thread (thread-name) &body body
Starts given piece of code in named thread, ensiring that reblocks/session::session and reblocks/request:request will be bound during it's execution.
Also, it set reblocks.websocket:backround to true, to make `update' method distinguish between usual request processing and background activity.
This variable becomes t during background processing.