From 0305ba0c6b45dfd8946104fa4150ff870210d363 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Thu, 21 Mar 2019 16:02:46 +0000 Subject: [PATCH] Merge pull request #1639 from sharelatex/bg-add-message-ids include a unique id in every message published to redis GitOrigin-RevId: f2843e5bb570247f03b260fa0f82f562c9c6014d --- .../Features/Editor/EditorRealTimeController.coffee | 9 +++++++++ .../coffee/Editor/EditorRealTimeControllerTests.coffee | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/services/web/app/coffee/Features/Editor/EditorRealTimeController.coffee b/services/web/app/coffee/Features/Editor/EditorRealTimeController.coffee index 5062533cbb..9d0a5e5084 100644 --- a/services/web/app/coffee/Features/Editor/EditorRealTimeController.coffee +++ b/services/web/app/coffee/Features/Editor/EditorRealTimeController.coffee @@ -1,13 +1,22 @@ Settings = require 'settings-sharelatex' RedisWrapper = require("../../infrastructure/RedisWrapper") rclient = RedisWrapper.client("realtime") +os = require "os" +crypto = require "crypto" + +HOST = os.hostname() +RND = crypto.randomBytes(4).toString('hex') # generate a random key for this process +COUNT = 0 module.exports = EditorRealTimeController = emitToRoom: (room_id, message, payload...) -> + # create a unique message id using a counter + message_id = "web:#{HOST}:#{RND}-#{COUNT++}" rclient.publish "editor-events", JSON.stringify room_id: room_id message: message payload: payload + _id: message_id emitToAll: (message, payload...) -> @emitToRoom "all", message, payload... diff --git a/services/web/test/unit/coffee/Editor/EditorRealTimeControllerTests.coffee b/services/web/test/unit/coffee/Editor/EditorRealTimeControllerTests.coffee index 3e811c1cfb..64c1329131 100644 --- a/services/web/test/unit/coffee/Editor/EditorRealTimeControllerTests.coffee +++ b/services/web/test/unit/coffee/Editor/EditorRealTimeControllerTests.coffee @@ -12,6 +12,8 @@ describe "EditorRealTimeController", -> client: () => @rclient "../../infrastructure/Server" : io: @io = {} "settings-sharelatex":{redis:{}} + "crypto": @crypto = { randomBytes: sinon.stub().withArgs(4).returns(Buffer.from([0x1, 0x2, 0x3, 0x4])) } + "os": @os = {hostname: sinon.stub().returns("somehost")} @room_id = "room-id" @message = "message-to-editor" @@ -19,6 +21,7 @@ describe "EditorRealTimeController", -> describe "emitToRoom", -> beforeEach -> + @message_id = "web:somehost:01020304-0" @EditorRealTimeController.emitToRoom(@room_id, @message, @payload...) it "should publish the message to redis", -> @@ -27,6 +30,7 @@ describe "EditorRealTimeController", -> room_id: @room_id, message: @message payload: @payload + _id: @message_id )) .should.equal true