Skip to content

Commit

Permalink
Add support for a binary serializer (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gazler authored Feb 4, 2025
1 parent bca50f8 commit 0fe98c4
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 134 deletions.
8 changes: 7 additions & 1 deletion lib/slipstream/connection/impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ defmodule Slipstream.Connection.Impl do
end

def push_message(message, state) do
push_message({:text, encode(message, state)}, state)
message =
case encode(message, state) do
{:binary, message} -> {:binary, message}
message when is_binary(message) -> {:text, message}
end

push_message(message, state)
end

# coveralls-ignore-start
Expand Down
7 changes: 6 additions & 1 deletion lib/slipstream/serializer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ defmodule Slipstream.Serializer do

@doc """
Encodes `Slipstream.Message` structs to binary.
Should return either a binary (string) when using a text based protocol
or `{:binary, binary}` for cases where a binary protocol is used over
the wire (such as MessagePack).
"""
@callback encode!(Slipstream.Message.t(), options :: Keyword.t()) :: binary()
@callback encode!(Slipstream.Message.t(), options :: Keyword.t()) ::
binary() | {:binary, binary()}

@doc """
Decodes binary into `Slipstream.Message` struct.
Expand Down
9 changes: 5 additions & 4 deletions test/fixtures/good_example.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ defmodule Slipstream.GoodExample do

use Slipstream, restart: :transient

@config Application.compile_env!(:slipstream, __MODULE__)

def start_link(opts) do
Slipstream.start_link(__MODULE__, opts)
end

@impl Slipstream
def init(test_proc) do
def init(opts) do
{test_proc, opts} = Keyword.pop!(opts, :pid)
opts = Keyword.put_new(opts, :uri, "ws://localhost:4001/socket/websocket")

socket =
@config
opts
|> connect!()
|> assign(:test_proc, test_proc)

Expand Down
6 changes: 3 additions & 3 deletions test/slipstream/client_telemetry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defmodule Slipstream.ClientTelemetryTest do
end

test "when we connect and disconnect, we get expected telemetry" do
start_supervised!({@client, self()})
start_supervised!({@client, pid: self()})
assert_receive {@client, :connected}

assert_receive {:telemetry, [:slipstream, :client, :connect, :start],
Expand All @@ -66,7 +66,7 @@ defmodule Slipstream.ClientTelemetryTest do

test "when we join a channel, we get expected telemetry" do
topic = "test:good"
pid = start_supervised!({@client, self()})
pid = start_supervised!({@client, pid: self()})
assert_receive {@client, :connected}
join(pid, topic)
assert_receive {@client, :joined, ^topic, %{}}
Expand All @@ -88,7 +88,7 @@ defmodule Slipstream.ClientTelemetryTest do

test "when we receive a message, we get expected telemetry" do
topic = "test:good"
pid = start_supervised!({@client, self()})
pid = start_supervised!({@client, pid: self()})
assert_receive {@client, :connected}
join(pid, topic)
assert_receive {@client, :joined, ^topic, %{}}
Expand Down
4 changes: 2 additions & 2 deletions test/slipstream/connection_telemetry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defmodule Slipstream.ConnectionTelemetryTest do
end

test "when we connect and disconnect, we get expected telemetry" do
start_supervised!({@client, self()})
start_supervised!({@client, pid: self()})
assert_receive {@client, :connected}

assert_receive {:telemetry, [:slipstream, :connection, :connect, :start],
Expand All @@ -68,7 +68,7 @@ defmodule Slipstream.ConnectionTelemetryTest do
end

test "when we successfully connect, we handle mint messages" do
start_supervised!({@client, self()})
start_supervised!({@client, pid: self()})
assert_receive {@client, :connected}

assert_receive {:telemetry, [:slipstream, :connection, :connect, :start],
Expand Down
Loading

0 comments on commit 0fe98c4

Please sign in to comment.