From a576f83dcc46eb1d10f7179545a5d6f9d284ef41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Ro=C5=BCnawski?= Date: Tue, 18 Jul 2023 15:27:14 +0200 Subject: [PATCH 1/2] Pass name to websockex --- README.md | 38 +++++++++++++++++++++++++++------ lib/jellyfish/notifier.ex | 44 ++++++++++++++++++++++++++------------- 2 files changed, 62 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index c9bb17b..728a889 100644 --- a/README.md +++ b/README.md @@ -23,18 +23,42 @@ def deps do end ``` +## Jellyfish connection configuration + +Define the connection configuration in the mix config, +specifying server address and authentication token +(for more information see [Jellyfish docs](https://jellyfish-dev.github.io/jellyfish-docs/getting_started/authentication)) +``` config.exs +config :jellyfish_server_sdk, + server_address: "localhost:5002", + server_api_token: "your-jellyfish-token", + secure?: true +``` + +Alternatively, the connection options can be provided when creating a `Jellyfish.Client` or starting `Jellyfish.Notifier` instead of using config: + +``` +client = + Jellyfish.Client.new(server_address: "localhost:5002", server_api_key: "your-jellyfish-token") + +{:ok, notifier} = + Jellyfish.Notifier.start( + server_address: "localhost:5002", + server_api_token: "your-jellyfish-token" + ) +``` + ## Usage -Make API calls to Jellyfish (authentication required, for more information see [Jellyfish docs](https://jellyfish-dev.github.io/jellyfish-docs/getting_started/authentication)) -and receive server events: +Make API calls to Jellyfish and receive server events: ```elixir # start process responsible for receiving events -{:ok, notifier} = Jellyfish.Notifier.start(server_address: "localhost:5002", server_api_key: "your-jellyfish-token") +{:ok, notifier} = Jellyfish.Notifier.start() {:ok, _rooms} = Jellyfish.Notifier.subscribe_server_notifications(notifier, :all) # create HTTP client instance -client = Jellyfish.Client.new(server_address: "localhost:5002", server_api_key: "your-jellyfish-token") +client = Jellyfish.Client.new() # Create room {:ok, %Jellyfish.Room{id: room_id}} = Jellyfish.Room.create(client, max_peers: 10) @@ -43,7 +67,8 @@ room_id # => "8878cd13-99a6-40d6-8d7e-8da23d803dab" # Add peer -{:ok, %Jellyfish.Peer{id: peer_id}, peer_token} = Jellyfish.Room.add_peer(client, room_id, Jellyfish.Peer.WebRTC) +{:ok, %Jellyfish.Peer{id: peer_id}, peer_token} = + Jellyfish.Room.add_peer(client, room_id, Jellyfish.Peer.WebRTC) receive do {:jellyfish, %Jellyfish.Notification.PeerConnected{room_id: ^room_id, peer_id: ^peer_id}} -> @@ -58,7 +83,8 @@ List of structs representing events can be found in the [docs](https://hexdocs.p ## Testing -When calling `mix test` it will automatically start the Jellyfish container under the hood. But tests on CI are run with the use of docker-compose, to run it locally in the same way as on CI you can use command `mix integration_test`. +When calling `mix test` it will automatically start the Jellyfish container under the hood. +Tests on CI are run with the use of docker-compose, to run it locally in the same way as on CI run `mix integration_test`. ## Copyright and License diff --git a/lib/jellyfish/notifier.ex b/lib/jellyfish/notifier.ex index b95c67e..54c6baf 100644 --- a/lib/jellyfish/notifier.ex +++ b/lib/jellyfish/notifier.ex @@ -3,21 +3,12 @@ defmodule Jellyfish.Notifier do Module defining a process responsible for establishing WebSocket connection and receiving events from Jellyfish server. - Define the connection configuration in the mix config - ``` config.exs - config :jellyfish_server_sdk, - server_address: "localhost:5002", - server_api_token: "your-jellyfish-token", - secure?: true - ``` + First, [configure the connection options](https://hexdocs.pm/jellyfish_server_sdk/readme.html#jellyfish-connection-configuration). ``` # Start the Notifier iex> {:ok, notifier} = Jellyfish.Notifier.start() {:ok, #PID<0.301.0>} - - # Optionally, you can provide connection options instead of using config: - {:ok, notifier} = Jellyfish.Notifier.start(server_address: "localhost:5002", server_api_token: "your-jellyfish-token") ``` ``` @@ -34,11 +25,17 @@ defmodule Jellyfish.Notifier do }} :ok ``` + + When starting the Notifier, you can provide the name under which the process will be registered. + ``` + iex> {:ok, notifier} = Jellyfish.Notifier.start_link(name: Jellyfish.Notifier) + ``` + """ use WebSockex - alias Jellyfish.{Client, Room, Utils} + alias Jellyfish.{Room, Utils} alias Jellyfish.{Notification, ServerMessage} alias Jellyfish.ServerMessage.{ @@ -61,6 +58,16 @@ defmodule Jellyfish.Notifier do """ @type notifier() :: GenServer.server() + @typedoc """ + Connection options used to connect to Jellyfish server. + """ + @type options() :: [ + server_address: String.t(), + server_api_token: String.t(), + secure?: boolean(), + name: GenServer.name() + ] + @doc """ Starts the Notifier process and connects to Jellyfish. @@ -68,7 +75,8 @@ defmodule Jellyfish.Notifier do See `start/1` for more information. """ - @spec start_link(Client.connection_options()) :: {:ok, pid()} | {:error, term()} + @spec start_link(options()) :: + {:ok, pid()} | {:error, term()} def start_link(opts \\ []) do connect(:start_link, opts) end @@ -80,7 +88,8 @@ defmodule Jellyfish.Notifier do For information about options, see `t:Jellyfish.Client.connection_options/0`. """ - @spec start(Client.connection_options()) :: {:ok, pid()} | {:error, term()} + @spec start(options()) :: + {:ok, pid()} | {:error, term()} def start(opts \\ []) do connect(:start, opts) end @@ -219,8 +228,15 @@ defmodule Jellyfish.Notifier do %ServerMessage{content: {:auth_request, %AuthRequest{token: api_token}}} |> ServerMessage.encode() + websockex_opts = Keyword.take(opts, [:name]) + with {:ok, ws} <- - apply(WebSockex, fun, ["#{address}/socket/server/websocket", __MODULE__, state]), + apply(WebSockex, fun, [ + "#{address}/socket/server/websocket", + __MODULE__, + state, + websockex_opts + ]), :ok <- WebSockex.send_frame(ws, {:binary, auth_msg}) do receive do {:jellyfish, :authenticated} -> From 94f64195cdfcb1f432a3347e9ae7e94f5ef77d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Ro=C5=BCnawski?= Date: Wed, 19 Jul 2023 15:46:53 +0200 Subject: [PATCH 2/2] Link to local file --- README.md | 2 +- lib/jellyfish/notifier.ex | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 728a889..fa163ad 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ config :jellyfish_server_sdk, secure?: true ``` -Alternatively, the connection options can be provided when creating a `Jellyfish.Client` or starting `Jellyfish.Notifier` instead of using config: +Alternatively, the connection options can be provided when creating a `Jellyfish.Client` or starting `Jellyfish.Notifier`: ``` client = diff --git a/lib/jellyfish/notifier.ex b/lib/jellyfish/notifier.ex index 54c6baf..8609f07 100644 --- a/lib/jellyfish/notifier.ex +++ b/lib/jellyfish/notifier.ex @@ -3,7 +3,7 @@ defmodule Jellyfish.Notifier do Module defining a process responsible for establishing WebSocket connection and receiving events from Jellyfish server. - First, [configure the connection options](https://hexdocs.pm/jellyfish_server_sdk/readme.html#jellyfish-connection-configuration). + First, [configure the connection options](README.md#jellyfish-connection-configuration). ``` # Start the Notifier @@ -75,8 +75,7 @@ defmodule Jellyfish.Notifier do See `start/1` for more information. """ - @spec start_link(options()) :: - {:ok, pid()} | {:error, term()} + @spec start_link(options()) :: {:ok, pid()} | {:error, term()} def start_link(opts \\ []) do connect(:start_link, opts) end @@ -88,8 +87,7 @@ defmodule Jellyfish.Notifier do For information about options, see `t:Jellyfish.Client.connection_options/0`. """ - @spec start(options()) :: - {:ok, pid()} | {:error, term()} + @spec start(options()) :: {:ok, pid()} | {:error, term()} def start(opts \\ []) do connect(:start, opts) end