Skip to content

Conversation

BoghosDavtyan
Copy link

Hello! As someone new to TypeScript, I've done my best to address an issue I was having with the music-together plugin and would appreciate your feedback.

Problem/Motivation

I noticed that when using the music-together feature, the connection would often drop randomly after a period of time (ranging from a few minutes to a few hours). This required manually re-entering the host ID on all connected clients, which was disruptive. The existing error handling didn't seem to account for recoverable network issues, and there wasn't much logging to help diagnose the problem.

Solution

I've modified the connection.ts file to build a more resilient connection handling system. The key changes are:

  • Automatic Reconnection Loop: Implemented a reconnectLoop that automatically tries to re-establish a connection to the PeerJS server if it's lost unexpectedly.
  • Smarter Error Handling: The main peer.on('error', ...) handler now distinguishes between recoverable network errors (like PeerUnavailable or ServerError) and fatal errors. It will only attempt to reconnect on recoverable errors.
  • State Management: Added isManualDisconnect and isReconnecting flags to ensure the reconnection logic only runs when it should (i.e., not after a user manually disconnects) and to prevent multiple loops from running at once.
  • Enhanced Debugging: Added several console.log and console.warn statements to provide clear feedback on the connection status, disconnection reasons, and reconnection attempts. This should make future troubleshooting much easier.

Testing

  • I have tested these changes extensively on Windows.
  • I hosted a music-together session and left it running for several hours, observing that it successfully recovered from network fluctuations without requiring a manual reconnect.
  • As I am new to TypeScript and have only tested on Windows, I would greatly appreciate feedback and testing on macOS and Linux to ensure there are no platform-specific issues.

Thank you for considering my contribution!

@JellyBrick JellyBrick requested a review from Copilot July 28, 2025 13:29
@JellyBrick JellyBrick added the enhancement New feature or request label Jul 28, 2025
Copilot

This comment was marked as outdated.

@JellyBrick JellyBrick requested a review from Copilot September 7, 2025 05:56
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR improves the reliability of the music-together plugin by implementing automatic reconnection logic to handle random disconnections that were disrupting user sessions.

Key changes:

  • Added automatic reconnection loop that attempts to re-establish connection when network issues occur
  • Implemented smarter error handling that distinguishes between recoverable and fatal errors
  • Added connection state management with flags to prevent multiple reconnection attempts and respect manual disconnections

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +107 to +129
while (!this.peer.destroyed) {
if (this.isManualDisconnect) break;

try {
if (!this.peer.disconnected) {
await delay(1000);
continue;
}

console.log(
'Music Together: Attempting to reconnect to PeerJS server...',
);
this.peer.reconnect();
break;
} catch (reconnectErr) {
console.error(
'Music Together: Reconnect attempt failed. Retrying in 10 seconds.',
reconnectErr,
);
// Wait before the next reconnection attempt
await delay(10000);
}
}
Copy link

Copilot AI Sep 7, 2025

Choose a reason for hiding this comment

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

This infinite loop could run indefinitely if the peer never becomes disconnected or destroyed, potentially consuming CPU resources. Consider adding a maximum retry count or timeout mechanism to prevent endless reconnection attempts.

Copilot uses AI. Check for mistakes.

this.waitOpen.reject(err);
this.connectionListeners.forEach((listener) => listener());
this.disconnect();
this.peer.on('close', () => reconnectLoop());
Copy link

Copilot AI Sep 7, 2025

Choose a reason for hiding this comment

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

The 'close' event typically indicates a deliberate disconnection, but this code triggers a reconnection attempt. This could cause unwanted reconnections when the peer is intentionally closed. Consider only triggering reconnectLoop on the 'disconnected' event or adding additional checks.

Suggested change
this.peer.on('close', () => reconnectLoop());
// Removed reconnectLoop on 'close' event to avoid unwanted reconnections.

Copilot uses AI. Check for mistakes.

Comment on lines +147 to +149
console.warn(
'Music Together: Disconnected from PeerJS server. The library will attempt to reconnect automatically.',
);
Copy link

Copilot AI Sep 7, 2025

Choose a reason for hiding this comment

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

The 'disconnected' event handler only logs a warning but doesn't trigger any reconnection logic. This inconsistency with the PR's stated goal of automatic reconnection could be confusing. Consider either triggering reconnectLoop here or removing the misleading warning message.

Suggested change
console.warn(
'Music Together: Disconnected from PeerJS server. The library will attempt to reconnect automatically.',
);
reconnectLoop();

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants