Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for a binary serializer #72

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading