reblocks-websocket
2024-10-12
Reblocks extension allowing to add a bidirectional communication via Websocket between a backend and Reblocks widgets.
Upstream URL
Author
License
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.
- Licence: Unlicense
- Author: Alexander Artemenko svetlyak.40wt@gmail.com
- Homepage: https://40ants.com/reblocks-websocket/
- Bug tracker: https://github.com/40ants/reblocks-websocket/issues
- Source control: GIT
- Depends on: alexandria, bordeaux-threads, jonathan, log4cl-extras, parenscript, reblocks, reblocks-parenscript, serapeum, websocket-driver
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.
Installation
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)
Usage
Define you widget and inherit it from the
reblocks-websocket:websocket-widget
:
(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.
API
REBLOCKS-WEBSOCKET
package reblocks-websocket
Classes
NO-ACTIVE-WEBSOCKETS
condition reblocks-websocket:no-active-websockets
(error)
WEBSOCKET-WIDGET
class reblocks-websocket:websocket-widget
(widget)
Functions
function reblocks-websocket:send-command
method-name &rest args
function reblocks-websocket:send-script
script
Sends JS
script to frontend via Websocket.
Macros
macro 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.
Variables
variable reblocks-websocket:*background*
nil
This variable becomes t during background processing.