]> git.cworth.org Git - zombocom-ai/commitdiff
Add peristence of comments
authorCarl Worth <cworth@cworth.org>
Wed, 7 Dec 2022 05:21:29 +0000 (21:21 -0800)
committerCarl Worth <cworth@cworth.org>
Wed, 7 Dec 2022 05:31:52 +0000 (21:31 -0800)
The server saves every message it receives, and replays them to each
new client when it joins.

Note: The server is only holding messages in memory, so there's not
yet any persistence across server restarts.

index.js

index 27b37147c1796569df0e625d8b88db2998e7175a..80526a3f6c879e14fb4a4055fe28504372f0d489 100644 (file)
--- a/index.js
+++ b/index.js
@@ -6,13 +6,21 @@ const { Server } = require("socket.io");
 const io = new Server(server);
 const port = 2122;
 
+comments = [];
+
 app.get('/index.html', (req, res) => {
     res.sendFile(__dirname + '/index.html');
 });
 
 io.on('connection', (socket) => {
-    socket.on('comment', (msg) => {
-        io.emit('comment', msg);
+    // Replay old comments to a newly-joining client
+    comments.forEach((comment) => {
+        socket.emit('comment', comment)
+    });
+    // When any client comments, send that to all clients (including sender)
+    socket.on('comment', (comment) => {
+        io.emit('comment', comment);
+        comments.push(comment);
     });
 });