Demo2 – Jarvis live demo (SSE, WebSockets and JSON)

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

Files

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

Prerequisites

Running

  1. 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/demo2
  2. Load 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.

  3. 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.

Stopping

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.

What to try

How it works

jarvisconfig.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:

Chat (in Chat.aplf) builds the same {"name":…,"text":…} JSON, with the time added, and sends it with #.w.Send.

Customising

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

Troubleshooting