Skip to content

Commit

Permalink
[RTC-274] Add RoomCreated and RoomDeleted notifications (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgfn committed Jul 13, 2023
1 parent c097290 commit be825a5
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "protos"]
path = protos
url = https://github.com/jellyfish-dev/protos.git
branch = master
path = protos
url = https://github.com/jellyfish-dev/protos.git
branch = master
5 changes: 5 additions & 0 deletions lib/jellyfish/room_service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ defmodule Jellyfish.RoomService do
state = put_in(state, [:rooms, room_pid], room_id)

Logger.info("Created room #{inspect(room.id)}")

Phoenix.PubSub.broadcast(Jellyfish.PubSub, "server_notification", {:room_created, room_id})

{:reply, {:ok, room}, state}
end

Expand Down Expand Up @@ -130,6 +133,8 @@ defmodule Jellyfish.RoomService do
try do
:ok = GenServer.stop(room, :normal)
Logger.info("Deleted room #{inspect(room_id)}")

Phoenix.PubSub.broadcast(Jellyfish.PubSub, "server_notification", {:room_deleted, room_id})
catch
:exit, {:noproc, {GenServer, :stop, [^room, :normal, :infinity]}} ->
Logger.warn("Room process with id #{inspect(room_id)} doesn't exist")
Expand Down
8 changes: 8 additions & 0 deletions lib/jellyfish_web/server_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ defmodule JellyfishWeb.ServerSocket do
PeerCrashed,
PeerDisconnected,
RoomCrashed,
RoomCreated,
RoomDeleted,
SubscribeRequest,
SubscriptionResponse
}
Expand Down Expand Up @@ -143,6 +145,12 @@ defmodule JellyfishWeb.ServerSocket do
def handle_info(msg, state) do
msg =
case msg do
{:room_created, room_id} ->
%ServerMessage{content: {:room_created, %RoomCreated{room_id: room_id}}}

{:room_deleted, room_id} ->
%ServerMessage{content: {:room_deleted, %RoomDeleted{room_id: room_id}}}

{:room_crashed, room_id} ->
%ServerMessage{content: {:room_crashed, %RoomCrashed{room_id: room_id}}}

Expand Down
26 changes: 26 additions & 0 deletions lib/protos/jellyfish/server_notifications.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,22 @@ defmodule Jellyfish.ServerMessage.SubscriptionResponse do
oneof: 0
end

defmodule Jellyfish.ServerMessage.RoomCreated do
@moduledoc false

use Protobuf, protoc_gen_elixir_version: "0.12.0", syntax: :proto3

field :room_id, 1, type: :string, json_name: "roomId"
end

defmodule Jellyfish.ServerMessage.RoomDeleted do
@moduledoc false

use Protobuf, protoc_gen_elixir_version: "0.12.0", syntax: :proto3

field :room_id, 1, type: :string, json_name: "roomId"
end

defmodule Jellyfish.ServerMessage do
@moduledoc false

Expand Down Expand Up @@ -266,4 +282,14 @@ defmodule Jellyfish.ServerMessage do
type: Jellyfish.ServerMessage.SubscriptionResponse,
json_name: "subscriptionResponse",
oneof: 0

field :room_created, 10,
type: Jellyfish.ServerMessage.RoomCreated,
json_name: "roomCreated",
oneof: 0

field :room_deleted, 11,
type: Jellyfish.ServerMessage.RoomDeleted,
json_name: "roomDeleted",
oneof: 0
end
2 changes: 1 addition & 1 deletion protos
25 changes: 25 additions & 0 deletions test/jellyfish_web/integration/server_socket_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ defmodule JellyfishWeb.Integration.ServerSocketTest do
PeerConnected,
PeerDisconnected,
RoomCrashed,
RoomCreated,
RoomDeleted,
SubscribeRequest,
SubscriptionResponse
}
Expand Down Expand Up @@ -205,6 +207,29 @@ defmodule JellyfishWeb.Integration.ServerSocketTest do
refute_receive %PeerConnected{}, 200
end

test "sends a message when room gets created and deleted", %{conn: conn} do
server_api_token = Application.fetch_env!(:jellyfish, :server_api_token)
ws = create_and_authenticate()

subscribe(
ws,
"1",
{:server_notification, %ServerNotification{room_id: {:option, :OPTION_ALL}}}
)

conn = put_req_header(conn, "authorization", "Bearer " <> server_api_token)

conn = post(conn, ~p"/room", maxPeers: 1)
assert %{"id" => room_id} = json_response(conn, :created)["data"]

assert_receive %RoomCreated{room_id: ^room_id}

conn = delete(conn, ~p"/room/#{room_id}")
assert response(conn, :no_content)

assert_receive %RoomDeleted{room_id: ^room_id}
end

test "sends a message when room crashes", %{conn: conn} do
server_api_token = Application.fetch_env!(:jellyfish, :server_api_token)
ws = create_and_authenticate()
Expand Down

0 comments on commit be825a5

Please sign in to comment.