Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Reconnect on network change #105

Merged
merged 15 commits into from
Jun 26, 2024
Merged

Reconnect on network change #105

merged 15 commits into from
Jun 26, 2024

Conversation

kamil-stasiak
Copy link
Contributor

@kamil-stasiak kamil-stasiak marked this pull request as ready for review June 19, 2024 10:14
@kamil-stasiak kamil-stasiak changed the title Reconnet on network change Reconnect on network change Jun 19, 2024
if (!client) return;

const onReconnectionStarted: ClientEvents<PeerMetadata, TrackMetadata>["reconnectionStarted"] = () => {
console.log("%c" + "reconnectionStarted", "color:green");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 niiice, I didn't know you can have colors in logs 🌈

@@ -69,6 +71,15 @@ export interface ClientEvents<PeerMetadata, TrackMetadata> {
/** Emitted when the connection is closed */
disconnected: (client: ClientApi<PeerMetadata, TrackMetadata>) => void;

/** Emitted when on successful reconnection */
reconnected: (client: ClientApi<PeerMetadata, TrackMetadata>) => void;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the user-facing API? I'd imagine in react we'd have a hook like useReconnection, something like this, feel free to change this is only an example:

const { isReconnecting, reconnectionFailed } = useReconnection();

So that user can display a nice notification that the app is currently trying to reconnect or reconnection failed. Imo it's more react-like than setting listeners.
Also google meet also tells me that the internet connection is unstable - maybe it's worth adding later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exposing events allows me to simplify a lot of logic in Videoroom; I removed a few useEffect hooks because of that.

And you are right that it's not the React way, so I've introduced the useReconnection hook with these fields if someone needs only the current state. So, events for state changes and a hook for the current state.

export type UseReconnection = {
  status: ReconnectionStatus;
  isReconnecting: boolean;
  isError: boolean;
  isIdle: boolean;
};

src/create.tsx Outdated
Comment on lines 425 to 430
if (
client.status === "joined" &&
event.mediaDeviceType === "userMedia" &&
!pending &&
!client.isReconnecting()
) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if is long and it's repeated and I don't know what exatly it means in general, maybe refactor it into a function or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've extracted this logic to separate functions isBroadcastedTrackChanged and isBroadcastedTrackStopped, refactored other fragments in the useSetupMedia hook, and extracted this hook to a separate file. I think the code could be simplified, but I don't want to do it right now.

@@ -61,6 +62,7 @@ const autostartAtom = atomWithStorage<boolean>("autostart", false, undefined, {

export const MainControls = () => {
const [token, setToken] = useAtom(tokenAtom);
useReconnectLogs();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this example is weird, is it a testing app or an example for the user? Because users generally don't need to log reconnection attempts into the console...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a testing app, but unfortunately it's located in the example directory. I've added a // for debugging comment to indicate that it's not necessary because I don't want to spend more time creating a whole new testing app.

@kamil-stasiak kamil-stasiak requested a review from graszka22 June 20, 2024 12:41
Comment on lines +46 to +49
status: ReconnectionStatus;
isReconnecting: boolean;
isError: boolean;
isIdle: boolean;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need isReconnecting, isError etc. if user can just:

if(status == 'reconnecting')

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for convenience

Comment on lines +25 to +28
status === "joined" &&
event.mediaDeviceType === expectedMediaDeviceType &&
event.trackType === expectedTrackType &&
stream;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this checks if track is stopped? 👀 I don't see anything related to stopping here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could implement it later

@kamil-stasiak kamil-stasiak requested a review from graszka22 June 26, 2024 13:34
@kamil-stasiak kamil-stasiak merged commit a69cdc1 into main Jun 26, 2024
4 checks passed
@kamil-stasiak kamil-stasiak deleted the reconnet-on-network-change branch June 26, 2024 13:37
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants