Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
websocket reimplementation of status requests
azivner992238f0b35e
diff --git a/bin/www b/bin/wwwindex b08686707c..d24928e781 100755--- a/bin/www+++ b/bin/www@@ -16,6 +16,7 @@ const https = require('https');const config = require('../services/config');const log = require('../services/log');const app_info = require('../services/app_info');+const messaging = require('../services/messaging');const port = normalizePort(config['Network']['port'] || '3000');app.set('port', port);@@ -23,7 +24,7 @@ app.set('port', port);/*** Create HTTP server.*/-let server;+let httpServer;if (config['Network']['https']) {const options = {@@ -31,12 +32,12 @@ if (config['Network']['https']) {cert: fs.readFileSync(config['Network']['certPath'])};- server = https.createServer(options, app);+ httpServer = https.createServer(options, app);log.info("App HTTPS server starting up at port " + port);}else {- server = http.createServer(app);+ httpServer = http.createServer(app);log.info("App HTTP server starting up at port " + port);}@@ -47,9 +48,11 @@ log.info(JSON.stringify(app_info, null, 2));* Listen on provided port, on all network interfaces.*/-server.listen(port);-server.on('error', onError);-server.on('listening', onListening);+httpServer.listen(port);+httpServer.on('error', onError);+httpServer.on('listening', onListening);++messaging.init(httpServer);/*** Normalize a port into a number, string, or false.@@ -106,7 +109,7 @@ function onError(error) {*/function onListening() {- const addr = server.address();+ const addr = httpServer.address();const bind = typeof addr === 'string'? 'pipe ' + addr: 'port ' + addr.port;diff --git a/package.json b/package.jsonindex bab7661da1..cb353148ee 100644--- a/package.json+++ b/package.json@@ -17,6 +17,7 @@"body-parser": "~1.18.2","cookie-parser": "~1.4.3","debug": "~3.1.0",+ "devtron": "^1.4.0","ejs": "~2.5.7","electron": "^1.8.2-beta.2","electron-debug": "^1.0.0",@@ -32,7 +33,7 @@"session-file-store": "^1.1.2","simple-node-logger": "^0.93.30","sqlite": "^2.8.0",- "devtron": "^1.4.0"+ "ws": "^3.3.2"},"devDependencies": {"electron-compile": "^6.4.2",diff --git a/public/javascripts/init.js b/public/javascripts/init.jsindex 1cb4b3eb9d..60df6451e5 100644--- a/public/javascripts/init.js+++ b/public/javascripts/init.js@@ -139,4 +139,31 @@ function initAjax() {});}-initAjax();\ No newline at end of file+initAjax();++// use wss for secure messaging+const ws = new WebSocket("ws://" + location.host);+ws.onopen = function (event) {+};++ws.onmessage = function (event) {+ console.log(event.data);++ const message = JSON.parse(event.data);++ if (message.type === 'sync') {+ const data = message.data;++ if (data.notes_tree) {+ console.log("Reloading tree because of background changes");++ noteTree.reload();+ }++ if (data.notes && data.notes.includes(noteEditor.getCurrentNoteId())) {+ showMessage('Reloading note because background change');++ noteEditor.reload();+ }+ }+};\ No newline at end of filediff --git a/public/javascripts/status.js b/public/javascripts/status.jsindex a35914b39d..fcd3a92d6b 100644--- a/public/javascripts/status.js+++ b/public/javascripts/status.js@@ -26,17 +26,17 @@ const status = (function() {}});- if (resp.changedTree) {- console.log("Reloading tree because of background changes");-- noteTree.reload();- }-- if (resp.changedCurrentNote) {- showMessage('Reloading note because background change');-- noteEditor.reload();- }+ // if (resp.changedTree) {+ // console.log("Reloading tree because of background changes");+ //+ // noteTree.reload();+ // }+ //+ // if (resp.changedCurrentNote) {+ // showMessage('Reloading note because background change');+ //+ // noteEditor.reload();+ // }changesToPushCountEl.html(resp.changesToPushCount);}diff --git a/services/messaging.js b/services/messaging.jsnew file mode 100644index 0000000000..90079af7b7--- /dev/null+++ b/services/messaging.js@@ -0,0 +1,26 @@+const utils = require('../services/utils');+const WebSocket = require('ws');++let webSocketServer;++function init(httpServer) {+ webSocketServer = new WebSocket.Server({server: httpServer});+ webSocketServer.on('connection', function connection(ws, req) {+ console.log("websocket client connected");+ });+}++async function send(message) {+ const jsonStr = JSON.stringify(message);++ webSocketServer.clients.forEach(function each(client) {+ if (client.readyState === WebSocket.OPEN) {+ client.send(jsonStr);+ }+ });+}++module.exports = {+ init,+ send+};\ No newline at end of filediff --git a/services/sync_table.js b/services/sync_table.jsindex e34b2d63c3..42a6cf6ae5 100644--- a/services/sync_table.js+++ b/services/sync_table.js@@ -1,6 +1,7 @@const sql = require('./sql');const source_id = require('./source_id');const utils = require('./utils');+const messaging = require('./messaging');async function addNoteSync(noteId, sourceId) {await addEntitySync("notes", noteId, sourceId)@@ -35,6 +36,41 @@ async function addEntitySync(entityName, entityId, sourceId) {});}+let startTime = utils.nowTimestamp();+let sentSyncId = [];++setInterval(async () => {+ const syncs = await sql.getResults("SELECT * FROM sync WHERE sync_date >= ?", [startTime]);+ startTime = utils.nowTimestamp();++ const data = {};+ const syncIds = [];++ for (const sync of syncs) {+ if (sentSyncId.includes(sync.id)) {+ continue;+ }++ if (!data[sync.entity_name]) {+ data[sync.entity_name] = [];+ }++ data[sync.entity_name].push(sync.entity_id);+ syncIds.push(sync.id);+ }++ if (syncIds.length > 0) {+ messaging.send({+ type: 'sync',+ data: data+ });++ for (const syncId of syncIds) {+ sentSyncId.push(syncId);+ }+ }+}, 1000);+module.exports = {addNoteSync,addNoteTreeSync,