Skip to content
Merged
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
38 changes: 32 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:

```
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)
Expand All @@ -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}} ->
Expand All @@ -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

Expand Down
42 changes: 28 additions & 14 deletions lib/jellyfish/notifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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](README.md#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")
```

```
Expand All @@ -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.{
Expand All @@ -61,14 +58,24 @@ 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.

Acts like `start/1` but links to the calling process.

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
Expand All @@ -80,7 +87,7 @@ 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
Expand Down Expand Up @@ -219,8 +226,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} ->
Expand Down