A single Jarvis server, running in JSON mode, that shows three ways for a browser to talk to Dyalog APL. A small web page exercises all three side by side.
| Channel | Direction | Endpoint | What it does |
|---|---|---|---|
| Server-Sent Events | server → client | GET /feed |
A live feed: a tick event
every few seconds carrying the time and the client counts |
| WebSocket | two-way | ws://host:8088/ |
A shared chat: each message is broadcast to every connected client |
| JSON | request/response | POST /stats |
Returns the current client counts, tick number and server time |
| File | Purpose |
|---|---|
Demo2.apln |
The application code (Demo2
namespace): Run, endpoint functions, WebSocket hooks and
the ticker |
jarvisconfig.json |
Jarvis configuration: port, code location, SSE and WebSocket settings |
web/index.html |
The browser front end (served by Jarvis
through the HTMLInterface setting) |
Jarvis.dyalog |
The Jarvis server class (version 1.23.1) |
WebSocketClient.aplc |
The WebSocketClient
class (version 1.0.2), used for Chat |
Chat.aplf |
Chat function: sends a chat
message from the APL session |
conga workspace that ships with Dyalog
(Run copies it in).Port in
jarvisconfig.json if not).Start Dyalog and make the demo2 folder the current
directory, for example with the ]CD user command, giving
the path to the folder on your machine:
]CD /path/to/demo2Load the demo and run it:
⎕FIX'file://Demo2.apln'
Demo2.Run
Run loads everything from the current directory and
starts the server:
'Conga'#.⎕CY'conga' ⍝ bring in shared Conga
2 #.⎕FIX'file://./Jarvis.dyalog'
2 #.⎕FIX'file://./WebSocketClient.aplc'
2 #.⎕FIX'file://./Chat.aplf'
#.j←#.Jarvis.New'jarvisconfig.json' ⍝ the server
#.j.Start
#.w←#.WebSocketClient.New'localhost:',⍕#.j.Port
#.w.Connect ⍝ a scripted chat client
The current directory must be demo2, because the files
and jarvisconfig.json are found relative to it.
Open http://localhost:8088/ in one or more browser windows.
Open the page in two windows to see the chat messages and the client counts update in both.
The last two lines of Run connect a scripted WebSocket
client #.w to the chat. Use Chat to post a
message from the APL session; it appears in every browser window:
'Brian' Chat 'Hello from APL' ⍝ left argument is the name (default 'anonymous')
Chat 'Hello again'
The browser page itself needs only Jarvis;
WebSocketClient and Chat are there for the
APL-side client.
Close the scripted chat client first, then stop the server:
#.w.Close ⍝ close the WebSocketClient connection
#.j.Stop ⍝ stop Jarvis; this also runs Shutdown, which ends the ticker thread
Browser windows still open on the page show “reconnecting…” / “closed” until the server is started again.
Live feed (SSE). Ticks appear in the Live feed
panel, each with its id, the time, and the number of SSE and WebSocket
clients. Stop and restart the server: the browser’s
EventSource reconnects automatically and sends the last
tick id back as Last-Event-ID, which the feed
function reports in its welcome message.
Chat (WebSocket). Enter a name and a message and
press Send, or use Chat from the APL session. The server
broadcasts it to every open window. New connections receive a “Welcome
to the chat” system message.
Stats (JSON). Press Get stats to
POST /stats and see the counts as JSON. You can also call
it from the command line:
curl -X POST -H 'Content-Type: application/json' -d '{}' http://localhost:8088/statsjarvisconfig.json wires the pieces together:
| Setting | Effect |
|---|---|
Paradigm: "JSON" |
Endpoint functions take and return JSON data |
CodeLocation: "Demo2.apln" |
The code to serve |
HTMLInterface: "web" |
Serve the static front end from the
web folder |
IncludeFns: "stats" |
Only stats is exposed as a
JSON endpoint; the hooks are not |
SSEEndpoints: "feed",
SSEHeartbeatInterval: 15 |
feed is a Server-Sent Events
endpoint, with a heartbeat every 15s |
EnableWebSockets,
OnWsUpgradeFn, OnWsReceiveFn,
OnWsCloseFn |
WebSocket hooks WsConnect,
WsReceive and WsClose |
AppInitFn: "Initialize",
AppCloseFn: "Shutdown" |
Start and stop the ticker thread with the server |
In Demo2.apln:
Run loads the demo’s code and starts the server, as
described above.Initialize saves the server reference and starts the
Ticker thread; Shutdown stops it.Ticker shows application code pushing SSE events from
outside any request: every TickerInterval seconds (5 by
default) it broadcasts a tick event to all
/feed connections.WsReceive parses the incoming JSON
({"name":…,"text":…}), removes extra blanks and limits the
length of the fields, and Broadcasts a chat
message to every client with WsSend, dropping connections
that have gone away.stats is an ordinary JSON endpoint.Chat (in Chat.aplf) builds the same
{"name":…,"text":…} JSON, with the time added, and sends it
with #.w.Send.
| To change… | Edit |
|---|---|
| The port | Port in
jarvisconfig.json |
| The tick rate | TickerInterval in
Demo2.apln |
| Chat name / message limits | The 40 limit and
500 limit calls in WsReceive |
| The page | web/index.html |
Port in
jarvisconfig.json, and open the page on the new port.FILE NAME ERROR or Run can’t find
a file: the session’s current directory isn’t
demo2. Use ]CD as in Running.Demo2.apln have no effect:
restart the server so the code is reloaded.