Skip to content
Run JavaScript in a browser from Common Lisp
Common Lisp
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Generic method stop was fixed for case when handler was not started yet. Jun 5, 2019
t Evaluate JS in the global context. Mar 13, 2018
.gitignore MVP Feb 19, 2016
.travis.yml Quick test Feb 21, 2016
README.md Update badge URL Mar 26, 2016
remote-js-test.asd Start the browser in a different thread Feb 21, 2016
remote-js.asd Get rid of an unused dependency. Jan 29, 2018

README.md

remote-js

Build Status

Send JavaScript from Common Lisp to a browser.

Overview

Usage

Simple Example

First, we create a context object:

(defvar ctx (remote-js:make-context))

Then we start the WebSockets server:

(remote-js:start ctx)

Now, remote-js gives us a function that generates the HTML of a simple page that connects to this context and notifies it when it's connected. We write the HTML to ~/test.html:

(with-open-file (stream (merge-pathnames #p"test.html" (user-homedir-pathname))
                        :direction :output
                        :if-exists :supersede
                        :if-does-not-exist :create)
  (write-string (remote-js:html ctx) stream))

Open the file in your browser. Now you can do:

(remote-js:eval ctx "alert('hello!')")

And you will see the alert box pop up in your browser.

Talking to the server

remote-js defines a function in the generated HTML, RemoteJS.send, which takes a string and sends it to the server. You can specify a callback for receiving messages like this:

(defvar ctx (remote-js:make-context
              :callback #'(lambda (message) (format t "Received: ~A~%" message))))

Then, start everything and generate the HTML file again:

(remote-js:start ctx)
(with-open-file (stream (merge-pathnames #p"test.html" (user-homedir-pathname))
                        :direction :output
                        :if-exists :supersede
                        :if-does-not-exist :create)
  (write-string (remote-js:html ctx) stream))

And open test.html in your browser.

Now you can send messages to the server like this:

CL-USER> (remote-js:eval ctx "RemoteJS.send('hi!')")
Received: hi!

Note: when a client connects to the server, it sends the string remote-js:+connected-message+.

Tests

The tests use trivial-open-browser, and running them will open your default browser to a temporary file.

License

Copyright (c) 2016 Fernando Borretti

Licensed under the MIT License.

You can’t perform that action at this time.