From 66d4f90b4ae7a894613cd449052e55ce54fa9c11 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Wed, 13 Apr 2022 14:39:07 +0200 Subject: [PATCH] Send the WebSocket message immediately with EventMachine.next_tick --- lib/runner/connection.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/runner/connection.rb b/lib/runner/connection.rb index 36f9555d..5c6ebc1d 100644 --- a/lib/runner/connection.rb +++ b/lib/runner/connection.rb @@ -53,7 +53,12 @@ class Runner::Connection def send_data(raw_data) encoded_message = encode(raw_data) Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Sending to #{@socket.url}: #{encoded_message.inspect}" } - @socket.send(encoded_message) + # Send the message to the WebSocket connection _immediately_ + # by scheduling it for the next execution of the EventMachine reactor run. + # Otherwise, the message might be queued causing delays for users. + EventMachine.next_tick do + @socket.send(encoded_message) + end end # Close the WebSocket connection