From e4678eaf513ed76e4743c6b780384de7d17ea764 Mon Sep 17 00:00:00 2001 From: Mandar Joshi Date: Sat, 14 Nov 2026 23:41:24 +0530 Subject: [PATCH] Support binary messages over the LongPoll transport --- assets/js/phoenix/longpoll.js | 16 ++++++- assets/test/longpoll_test.js | 20 ++++++--- lib/phoenix/transports/long_poll_server.ex | 20 ++++++--- .../integration/long_poll_channels_test.exs | 44 ++++++++++++++++++- 4 files changed, 86 insertions(+), 14 deletions(-) diff --git a/assets/js/phoenix/longpoll.js b/assets/js/phoenix/longpoll.js index 392fe43e7f..346282ffd7 100644 --- a/assets/js/phoenix/longpoll.js +++ b/assets/js/phoenix/longpoll.js @@ -15,6 +15,20 @@ let arrayBufferToBase64 = (buffer) => { return btoa(binary) } +let base64ToArrayBuffer = (base64) => { + let binary = atob(base64) + let len = binary.length + let bytes = new Uint8Array(len) + for(let i = 0; i < len; i++){ bytes[i] = binary.charCodeAt(i) } + return bytes.buffer +} + +// Text messages are always JSON, while binary messages are base64 encoded +// by the server, the same way we send them in batchSend. +let decodeMessage = (msg) => { + return msg[0] === "[" || msg[0] === "{" ? msg : base64ToArrayBuffer(msg) +} + export default class LongPoll { constructor(endPoint, protocols){ @@ -119,7 +133,7 @@ export default class LongPoll { // handler disconnects, which cancels the remaining messages. const timer = setTimeout(() => { this.messageTimers.delete(timer) - this.onmessage({data: msg}) + this.onmessage({data: decodeMessage(msg)}) }, 0) this.messageTimers.add(timer) }) diff --git a/assets/test/longpoll_test.js b/assets/test/longpoll_test.js index 98f7d009d6..2b99f656d6 100644 --- a/assets/test/longpoll_test.js +++ b/assets/test/longpoll_test.js @@ -250,12 +250,22 @@ describe("LongPoll", () => { afterEach(() => jest.useRealTimers()) it("delivers each message in its own task", () => { - Ajax.request.mock.calls[1][6]({status: 200, token: "token", messages: ["first", "second"]}) + Ajax.request.mock.calls[1][6]({status: 200, token: "token", messages: ["[\"first\"]", "[\"second\"]"]}) expect(events).toEqual([]) jest.advanceTimersByTime(0) - expect(events).toEqual(["first", "second"]) + expect(events).toEqual(["[\"first\"]", "[\"second\"]"]) + }) + + it("delivers base64 encoded messages as binary", () => { + Ajax.request.mock.calls[1][6]({status: 200, token: "token", messages: ["[\"text\"]", "/wEC", "{}"]}) + jest.advanceTimersByTime(0) + + expect(events[0]).toEqual("[\"text\"]") + expect(events[1]).toBeInstanceOf(ArrayBuffer) + expect(Array.from(new Uint8Array(events[1]))).toEqual([255, 1, 2]) + expect(events[2]).toEqual("{}") }) it("does not deliver messages after a POST timeout closed the transport", () => { @@ -263,7 +273,7 @@ describe("LongPoll", () => { jest.advanceTimersByTime(0) const post = Ajax.request.mock.calls.find(([method]) => method === "POST") - Ajax.request.mock.calls[1][6]({status: 200, token: "token", messages: ["message"]}) + Ajax.request.mock.calls[1][6]({status: 200, token: "token", messages: ["[\"message\"]"]}) post[5]() jest.advanceTimersByTime(0) @@ -276,10 +286,10 @@ describe("LongPoll", () => { longpoll.close() } - Ajax.request.mock.calls[1][6]({status: 200, token: "token", messages: ["first", "second"]}) + Ajax.request.mock.calls[1][6]({status: 200, token: "token", messages: ["[\"first\"]", "[\"second\"]"]}) jest.advanceTimersByTime(0) - expect(events).toEqual(["first", "close"]) + expect(events).toEqual(["[\"first\"]", "close"]) }) }) diff --git a/lib/phoenix/transports/long_poll_server.ex b/lib/phoenix/transports/long_poll_server.ex index e3a5f23d8d..3045973b76 100644 --- a/lib/phoenix/transports/long_poll_server.ex +++ b/lib/phoenix/transports/long_poll_server.ex @@ -49,11 +49,11 @@ defmodule Phoenix.Transports.LongPoll.Server do %{handler: {handler, handler_state}} = state case handler.handle_in({body, opcode: opcode}, handler_state) do - {:reply, status, {_, reply}, handler_state} -> + {:reply, status, {opcode, reply}, handler_state} -> state = %{state | handler: {handler, handler_state}} status = if status == :ok, do: :ok, else: :error broadcast_from!(state, client_ref, {status, ref}) - publish_reply(state, reply) + publish_reply(state, opcode, reply) {:ok, handler_state} -> state = %{state | handler: {handler, handler_state}} @@ -106,9 +106,9 @@ defmodule Phoenix.Transports.LongPoll.Server do %{handler: {handler, handler_state}} = state case handler.handle_info(message, handler_state) do - {:push, {_, reply}, handler_state} -> + {:push, {opcode, reply}, handler_state} -> state = %{state | handler: {handler, handler_state}} - publish_reply(state, reply) + publish_reply(state, opcode, reply) {:ok, handler_state} -> state = %{state | handler: {handler, handler_state}} @@ -132,16 +132,22 @@ defmodule Phoenix.Transports.LongPoll.Server do defp broadcast_from!(_state, client_ref, msg) when is_pid(client_ref), do: send(client_ref, msg) - defp publish_reply(state, reply) when is_map(reply) do + # Messages are sent to the client inside a JSON list, so binary + # messages are base64 encoded, the same way the client sends them. + defp publish_reply(state, :binary, reply) do + publish_reply(state, :text, reply |> IO.iodata_to_binary() |> Base.encode64()) + end + + defp publish_reply(state, _opcode, reply) when is_map(reply) do IO.warn( "Returning a map from the LongPolling serializer is deprecated. " <> "Please return JSON encoded data instead (see Phoenix.Socket.Serializer)" ) - publish_reply(state, Phoenix.json_library().encode_to_iodata!(reply)) + publish_reply(state, :text, Phoenix.json_library().encode_to_iodata!(reply)) end - defp publish_reply(state, reply) do + defp publish_reply(state, _opcode, reply) do notify_client_now_available(state) {:noreply, update_in(state.buffer, &[IO.iodata_to_binary(reply) | &1])} end diff --git a/test/phoenix/integration/long_poll_channels_test.exs b/test/phoenix/integration/long_poll_channels_test.exs index 2513f61b66..6725f7affa 100644 --- a/test/phoenix/integration/long_poll_channels_test.exs +++ b/test/phoenix/integration/long_poll_channels_test.exs @@ -45,6 +45,11 @@ defmodule Phoenix.Integration.LongPollChannelsTest do {:noreply, socket} end + def handle_in("echo_bin", {:binary, bin}, socket) do + push(socket, "bin_push", {:binary, bin}) + {:reply, {:ok, {:binary, bin}}, socket} + end + def handle_in("new_msg", message, socket) do broadcast!(socket, "new_msg", message) {:noreply, socket} @@ -215,6 +220,14 @@ defmodule Phoenix.Integration.LongPollChannelsTest do decode_body(serializer, resp) end + defp poll_messages(path, vsn, session, count, acc \\ []) do + resp = poll(:get, path, vsn, session) + assert resp.body["status"] == 200 + acc = acc ++ resp.body["messages"] + + if length(acc) < count, do: poll_messages(path, vsn, session, count, acc), else: acc + end + defp serializer("2." <> _, json), do: {V2.JSONSerializer, json} defp serializer(_, nil), do: {V1.JSONSerializer, nil} @@ -231,7 +244,11 @@ defmodule Phoenix.Integration.LongPollChannelsTest do |> update_in([:body], &Phoenix.json_library().decode!(&1)) |> update_in([:body, "messages"], fn messages -> for msg <- messages || [] do - serializer.decode!(msg, opcode: :text) + case msg do + "[" <> _ -> serializer.decode!(msg, opcode: :text) + "{" <> _ -> serializer.decode!(msg, opcode: :text) + base64 -> {:binary, Base.decode64!(base64)} + end end end) end @@ -753,6 +770,31 @@ defmodule Phoenix.Integration.LongPollChannelsTest do ] = resp.body["messages"] end + test "#{@mode}: pushes and replies with binary messages" do + vsn = "2.0.0" + session = join("/ws", "room:lobby", vsn, "1", @mode) + + resp = + poll(:post, "/ws", vsn, session, [ + %{ + "topic" => "room:lobby", + "event" => "echo_bin", + "join_ref" => "1", + "ref" => "2", + "payload" => {:binary, <<255, 1, 2>>} + } + ]) + + assert resp.body["status"] == 200 + + # The push and the reply may arrive in different polls + assert [_phx_reply, _user_entered, _joined, {:binary, push}, {:binary, reply}] = + poll_messages("/ws", vsn, session, 5) + + assert push == <<0, 1, 10, 8, "1", "room:lobby", "bin_push", 255, 1, 2>> + assert reply == <<1, 1, 1, 10, 2, "1", "2", "room:lobby", "ok", 255, 1, 2>> + end + test "#{@mode}: shuts down after timeout" do session = join("/ws", "room:lobby", @vsn, "1", @mode)