Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECO-4841] Add README section to explain "Connection limit exceeded" error and solutions #1905

Merged
merged 1 commit into from
Nov 4, 2024

Conversation

VeskeR
Copy link
Contributor

@VeskeR VeskeR commented Oct 23, 2024

Resolves ECO-4841

This is based on the "max connections errors" research done in ECO-4841 (see comments), "Ably-js max connections issue overview and possible solutions", and solutions found in ably-labs/react-hooks#8 (comment) and #1742 (comment)

Related: #1779, #1742

Summary by CodeRabbit

  • New Features

    • Added guidance for managing the "Connection limit exceeded" error in the README.
    • Introduced new hooks: useConnectionStateListener, useChannelStateListener, and useAbly.
    • New parameter skip added to useChannel, usePresence, and usePresenceListener hooks for better control over attachment behavior.
  • Documentation

    • Enhanced clarity and expanded usage examples for ably-js React Hooks.
    • Updated error handling section with detailed management strategies for connection and channel errors.
    • Added a section addressing common warnings when using the library with Next.js.

@VeskeR VeskeR requested review from ttypic and owenpearson October 23, 2024 12:12
Copy link

coderabbitai bot commented Oct 23, 2024

Note

Reviews paused

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Walkthrough

The changes introduced in this pull request enhance the documentation for the Ably JavaScript client library, specifically targeting the README.md and docs/react.md files. A new section addressing the "Connection limit exceeded" error has been added to the README, providing insights and recommendations for managing client instances, particularly in React and Next.js contexts. The React documentation has been improved with new sections on connection management, expanded usage examples, and enhanced error handling for hooks.

Changes

File Change Summary
README.md Added section on "Connection limit exceeded" error, causes, and recommendations for client management.
docs/react.md Updated React Hooks documentation with new sections on instance management, usage examples, and error handling; added new hooks: useConnectionStateListener, useChannelStateListener, useAbly; introduced skip parameter in hooks.

Assessment against linked issues

Objective Addressed Explanation
Update React getting started documentation (ECO-4841)

Possibly related PRs

  • [ECO-4914] Fix websocket reconnection can get stuck in disconnected/connecting cycle #1855: The changes in this PR improve WebSocket reconnection logic, which is relevant to managing client instances effectively to avoid connection limits as discussed in the main PR.
  • Release 2.3.2 #1857: This release includes bug fixes related to WebSocket reconnection, which ties into the main PR's focus on managing connections and preventing issues during development.
  • Release 2.4.0 #1863: This release introduces enhancements that may relate to connection management, aligning with the main PR's emphasis on effective client instantiation.
  • Release 2.4.1 #1885: This release addresses an issue with the usePresence hook, which is relevant to the main PR's focus on managing client instances and connection limits in React applications.

Suggested reviewers

  • ttypic
  • lawrence-forooghian
  • umair-ably

🐇 In the code we hop and play,
New docs to guide us on our way.
With connections managed, oh so bright,
React’s hooks now feel just right!
So let’s rejoice and give a cheer,
For clearer paths, we hold so dear! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot temporarily deployed to staging/pull/1905/bundle-report October 23, 2024 12:13 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1905/features October 23, 2024 12:13 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1905/typedoc October 23, 2024 12:13 Inactive
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (6)
docs/react.md (4)

39-39: Enhance connection management guidance with examples

While the guidance about creating the Ably.Realtime instance outside components is correct, consider adding concrete examples to illustrate:

  1. ❌ Incorrect implementation (inside component)
  2. ✅ Correct implementation (outside component)

This visual comparison would make it easier for developers to understand and avoid the connection limit issue.

Example addition:

 Also, ensure that the `Ably.Realtime` instance is created outside of components to prevent it from being recreated on component re-renders. This will help avoid opening extra unnecessary connections to the Ably servers and potentially reaching the maximum connections limit on your account.
+
+```jsx
+// ❌ Incorrect: Creates new connection on every render
+function BadComponent() {
+  const client = new Ably.Realtime({ key: 'api-key' }); // Don't do this!
+  return <div>...</div>;
+}
+
+// ✅ Correct: Single connection instance
+const client = new Ably.Realtime({ key: 'api-key' });
+function GoodComponent() {
+  return <div>...</div>;
+}
+```

Line range hint 391-417: Add TypeScript types and common error scenarios

The error handling section is comprehensive but could be enhanced with:

  1. TypeScript type definitions for error objects
  2. Common error scenarios and their solutions

Consider adding:

 const { connectionError, channelError } = useChannel('my_channel', messageHandler);
+
+// TypeScript type hints
+type ConnectionError = Ably.ErrorInfo & {
+  // Add specific connection error properties
+};
+
+type ChannelError = Ably.ErrorInfo & {
+  // Add specific channel error properties
+};
+
+// Common error scenarios
+/**
+ * Common Connection Errors:
+ * - 40142: Connection limit exceeded
+ *   Solution: Ensure Ably.Realtime instance is created outside components
+ * - 40171: Invalid API key
+ *   Solution: Check API key format and permissions
+ *
+ * Common Channel Errors:
+ * - 40160: Channel operation not permitted
+ *   Solution: Verify channel permissions in API key capabilities
+ */

Line range hint 418-450: Add practical examples for state listener hooks

The documentation for new hooks would benefit from real-world examples showing:

  1. Integration with error handling
  2. Common use cases like displaying connection status

Consider adding:

 useConnectionStateListener((stateChange) => {
   console.log(stateChange.current);
   console.log(stateChange.previous);
   console.log(stateChange.reason);
 });
+
+// Real-world example: Connection status indicator
+const ConnectionStatus = () => {
+  const [status, setStatus] = useState('connected');
+  const [error, setError] = useState(null);
+
+  useConnectionStateListener((stateChange) => {
+    setStatus(stateChange.current);
+    if (stateChange.reason) {
+      setError(stateChange.reason.message);
+    }
+  });
+
+  return (
+    <div>
+      <span className={`status-${status}`}>
+        {status}
+      </span>
+      {error && <p className="error">{error}</p>}
+    </div>
+  );
+};

Line range hint 543-583: Add version compatibility and related documentation links

Consider enhancing the NextJS warnings section with:

  1. NextJS version compatibility information
  2. Links to related NextJS documentation about webpack configuration

Add version information and links:

 ## NextJS warnings
+
+> Tested with NextJS versions 12.x and 13.x
+> For more information about NextJS webpack configuration, see the [official documentation](https://nextjs.org/docs/api-reference/next.config.js/custom-webpack-config)
README.md (2)

648-657: Consider adding TypeScript type annotations to the code example.

The explanation and solution are excellent. To make it even more helpful, consider adding TypeScript types to demonstrate type-safe usage:

-const client = new Ably.Realtime({ key: 'your-ably-api-key', autoConnect: typeof window !== 'undefined' });
+const client: Ably.Realtime = new Ably.Realtime({ 
+  key: 'your-ably-api-key', 
+  autoConnect: typeof window !== 'undefined' 
+});
🧰 Tools
🪛 LanguageTool

[uncategorized] ~648-~648: If this is a compound adjective that modifies the following noun, use a hyphen.
Context: ...'re developing your app, due to Next.js server side rendering your components. Note that ev...

(EN_COMPOUND_ADJECTIVE_INTERNAL)


[uncategorized] ~656-~656: If this is a compound adjective that modifies the following noun, use a hyphen.
Context: ...e server side, while not affecting your client side components. - If you're using any Reac...

(EN_COMPOUND_ADJECTIVE_INTERNAL)

🪛 Markdownlint

652-652: Expected: indented; Actual: fenced
Code block style

(MD046, code-block-style)


660-663: Consider adding a code example for the recommended file structure.

The explanation of the HMR issue and solution is excellent. To make it even more helpful, consider adding a code example showing the recommended file structure:

// ably-client.ts
import * as Ably from 'ably';

export const ablyClient = new Ably.Realtime({ 
  key: 'your-ably-api-key',
  autoConnect: typeof window !== 'undefined'
});

// your-component.tsx
import { ablyClient } from './ably-client';

// Use ablyClient in your component
🧰 Tools
🪛 LanguageTool

[uncategorized] ~662-~662: Although a hyphen is possible, it is not necessary in a compound modifier in which the first word is an adverb that ends in ‘ly’.
Context: ...ime()call to a separate file, such asably-client.js`, and export the client instance fro...

(HYPHENATED_LY_ADVERB_ADJECTIVE)


[style] ~662-~662: Consider shortening or rephrasing this to strengthen your wording.
Context: ...only be recreated when you specifically make changes to the ably-client.js file, which should...

(MAKE_CHANGES)


[uncategorized] ~662-~662: Although a hyphen is possible, it is not necessary in a compound modifier in which the first word is an adverb that ends in ‘ly’.
Context: ...en you specifically make changes to the ably-client.js file, which should be far less freq...

(HYPHENATED_LY_ADVERB_ADJECTIVE)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 0bd7aa5 and af29f55.

📒 Files selected for processing (2)
  • README.md (1 hunks)
  • docs/react.md (1 hunks)
🧰 Additional context used
🪛 LanguageTool
README.md

[uncategorized] ~648-~648: If this is a compound adjective that modifies the following noun, use a hyphen.
Context: ...'re developing your app, due to Next.js server side rendering your components. Note that ev...

(EN_COMPOUND_ADJECTIVE_INTERNAL)


[uncategorized] ~656-~656: If this is a compound adjective that modifies the following noun, use a hyphen.
Context: ...e server side, while not affecting your client side components. - If you're using any Reac...

(EN_COMPOUND_ADJECTIVE_INTERNAL)


[style] ~658-~658: This phrase is redundant. Consider using “outside”.
Context: ...nstantiation call (new Ably.Realtime) outside of the components. You can find an example...

(OUTSIDE_OF)


[uncategorized] ~662-~662: Although a hyphen is possible, it is not necessary in a compound modifier in which the first word is an adverb that ends in ‘ly’.
Context: ...ime()call to a separate file, such asably-client.js`, and export the client instance fro...

(HYPHENATED_LY_ADVERB_ADJECTIVE)


[style] ~662-~662: Consider shortening or rephrasing this to strengthen your wording.
Context: ...only be recreated when you specifically make changes to the ably-client.js file, which should...

(MAKE_CHANGES)


[uncategorized] ~662-~662: Although a hyphen is possible, it is not necessary in a compound modifier in which the first word is an adverb that ends in ‘ly’.
Context: ...en you specifically make changes to the ably-client.js file, which should be far less freq...

(HYPHENATED_LY_ADVERB_ADJECTIVE)

🪛 Markdownlint
README.md

652-652: Expected: indented; Actual: fenced
Code block style

(MD046, code-block-style)

🔇 Additional comments (3)
docs/react.md (1)

Line range hint 451-542: LGTM! Comprehensive documentation of the skip parameter

The documentation effectively covers:

  • Purpose and behavior of the skip parameter
  • Real-world use cases with examples
  • Integration with authentication and premium features
README.md (2)

644-647: LGTM! Clear and informative introduction.

The section header and introduction effectively set the context for the common "Connection limit exceeded" error.


658-659: LGTM! Clear explanation with helpful reference.

The explanation effectively communicates the React-specific issue and points users to detailed examples in the React documentation.

🧰 Tools
🪛 LanguageTool

[style] ~658-~658: This phrase is redundant. Consider using “outside”.
Context: ...nstantiation call (new Ably.Realtime) outside of the components. You can find an example...

(OUTSIDE_OF)

@VeskeR
Copy link
Contributor Author

VeskeR commented Oct 23, 2024

@coderabbitai pause

Copy link

coderabbitai bot commented Oct 23, 2024

✅ Actions performed

Reviews paused.

@VeskeR VeskeR force-pushed the ECO-4841/document-max-connections-issue-solutions branch from af29f55 to 2d3d1e8 Compare October 23, 2024 12:28
@github-actions github-actions bot temporarily deployed to staging/pull/1905/bundle-report October 23, 2024 12:28 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1905/features October 23, 2024 12:29 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1905/typedoc October 23, 2024 12:29 Inactive
Copy link
Member

@owenpearson owenpearson left a comment

Choose a reason for hiding this comment

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

lgtm, one minor suggestion

README.md Show resolved Hide resolved
@VeskeR VeskeR requested a review from owenpearson October 24, 2024 17:32
Copy link
Collaborator

@ttypic ttypic left a comment

Choose a reason for hiding this comment

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

LGTM

@VeskeR VeskeR merged commit 137a8a7 into main Nov 4, 2024
12 of 14 checks passed
@VeskeR VeskeR deleted the ECO-4841/document-max-connections-issue-solutions branch November 4, 2024 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants