There was a possibility of a race condition where two unrelated messages could have been written at the same time to the outgoing websockets connection, it was brought to my attention some time ago by @ccl0326.
This patch fixes the problem using channels for synchronization.