Upgrade to TiddlyWiki 5.1.17
Note: WebSockets support will be revisited in a future release.
This release mainly upgrades to 5.1.17. It also finishes implementing the websocket server handling for data folders.
If a page requests a path that is not a data folder, it will be stored in a separate hashmap of such paths. Paths may be arbitrary, but only the actual path part of the URL counts -- not the query part. Paths under a data folder (i.e. if a data folder is mounted at /wikis/notes/mynotes
, a WS URL pointing to /wikis/notes/mynotes/random
) will be handed off to the data folder TiddlyWiki server handler.
This allows plugins to use their own channels (subpaths) under the data folder path. Care should be taken to name the path appropriately. Using the plugin's name is recommended.
/wikis/notes/mynotes/plugins/arlen22/helloworldplugin/and/more/levels/if/you/want
And plugins should not use paths outside the datafolder. The best way to determine the URL to use is simply to take the page URL and add to it.
new WebSocket("ws://" + location.host + location.pathname + "/plugins/arlen22/helloworldplugin/etc")
There is no client-side WebSocket object in TiddlyWiki. I don't think we need one. Client-side code can handle that itself.
On the server-side, the ws-client-connect
message is sent whenever a client connects, and includes the client, HTTP request, and data folder sub path the client requested. This allows plugin code to select which clients they want to handle based on the request and subpath.
There is a global listener that listens to all clients and sends ws-client-message
events which contain the message and a reference to the client. Plugin code may check if the client is in its own list of clients to handle or examine the contents of the message to determine whether or not to handle it.
A ws-client-close
message will be sent when a client is no longer used. Plugin code should then remove all references to the client to allow garbage collection.
This is assuming that pull request TiddlyWiki/TiddlyWiki5#3289 gets merged in. So the specification is complete and I've implemented TiddlyServer's part, but TiddlyWiki 5.1.17 does not yet contain all the code necessary for this to work. However, it is in the pull request, and will hopefully be there by 5.1.18. TiddlyServer itself only implements the ws-client-connect
event and non-datafolder requests in order to avoid code duplication.
Plugins may take advantage of the websocket server by calling $tw.hooks.addHook('th-server-command-post-start', (simpleServer, eventEmitter, serverPlatform) => void);
.
The simpleServer
handles routing and contains the pathprefix (simpleServer.get("pathprefix")
if desired). The eventEmitter
emits the events I mentioned above, and may also be accessed at $tw.wss
after the hook finishes. The serverPlatform is a string specifying which server is in use (currently tiddlyserver
or tiddlywiki
).