Mastodon: Migrate from discontinued cws to uWebSockets.js or similar

Created on 19 Nov 2020  路  5Comments  路  Source: tootsuite/mastodon

Expected behaviour

Streaming server should run on current LTS (node v14).

Actual behaviour

As shown in #15151, the current cws module fails and is not supported.
Streaming server is broken https://paste.ubuntu.com/p/BDpfDTSqVV/

Steps to reproduce the problem

Run master on v14 LTS node.

Specifications

3.2.1/Current Master

bug

All 5 comments

Hm, the APIs are not the same at all. It will require rewriting a lot of the server...

Uh-oh https://medium.com/@rockstudillo/beware-of-uwebsockets-js-b51c92cac83f

Boo. Shame nobody just forked the existing cws... Gotta love node ecosystem https://www.npmjs.com/package/ws

I sure wonder if all the drama from uws and co is worth switching from plain ws.


Also this diff is enough to get the current (3.2.1) streaming to use ws (mostly just switching back the server and reimplement the previously replaced ping).

diff --git a/streaming/index.js b/streaming/index.js
index 39e70c1ba..70f2e2fb2 100644
--- a/streaming/index.js
+++ b/streaming/index.js
@@ -7,9 +7,9 @@ const redis = require('redis');
 const pg = require('pg');
 const log = require('npmlog');
 const url = require('url');
-const { WebSocketServer } = require('@clusterws/cws');
 const uuid = require('uuid');
 const fs = require('fs');
+const WebSocket = require('ws');

 const env = process.env.NODE_ENV || 'development';
 const alwaysRequireAuth = process.env.WHITELIST_MODE === 'true' || process.env.AUTHORIZED_FETCH === 'true';
@@ -614,13 +614,19 @@ const startWorker = (workerId) => {
     });
   });

-  const wss = new WebSocketServer({ server, verifyClient: wsVerifyClient });
+  const wss = new WebSocket.Server({ server, verifyClient: wsVerifyClient });

   wss.on('connection', (ws, req) => {
     const location = url.parse(req.url, true);
     req.requestId  = uuid.v4();
     req.remoteAddress = ws._socket.remoteAddress;

+    ws.isAlive = true;
+
+    ws.on('pong', () => {
+      ws.isAlive = true;
+    });
+
     let channel;

     switch(location.query.stream) {
@@ -692,7 +698,17 @@ const startWorker = (workerId) => {
     }
   });

-  wss.startAutoPing(30000);
+  setInterval(() => {
+    wss.clients.forEach(ws => {
+      if (ws.isAlive === false) {
+        ws.terminate();
+        return;
+      }
+
+      ws.isAlive = false;
+      ws.ping('', false, true);
+    });
+  }, 30000);

   attachServerWithConfig(server, address => {
     log.info(`Worker ${workerId} now listening on ${address}`);

It might be worth putting a note in the https://docs.joinmastodon.org/dev/setup/ page that Node v14/15 aren't supported for now.

Was this page helpful?
0 / 5 - 0 ratings