Skip to content

[EDU-1845] Pub/Sub getting started in JavaScript #2511

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

m-hulbert
Copy link
Contributor

Description

This PR adds a new getting started guide for Ably Pub/Sub in JavaScript.

It includes;

  • A new getting started guide for Pub/Sub in JavaScript.
  • Adds the guide to the existing 'getting started' section for now. Other items will be moved/removed once there are more guides.
  • Adjusts the padding on h2, ol and ul elements for readability in the getting started guide.

Checklist

@m-hulbert m-hulbert self-assigned this Apr 8, 2025
Copy link

coderabbitai bot commented Apr 8, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

Copy link
Contributor

@GregHolmes GregHolmes left a comment

Choose a reason for hiding this comment

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

Just a few comments.

@m-hulbert m-hulbert temporarily deployed to ably-docs-edu-1845-pubs-jstarl April 9, 2025 12:47 Inactive
@m-hulbert m-hulbert requested a review from GregHolmes April 9, 2025 12:48
@m-hulbert m-hulbert temporarily deployed to ably-docs-edu-1845-pubs-jstarl April 9, 2025 13:27 Inactive
Copy link
Contributor

@mschristensen mschristensen left a comment

Choose a reason for hiding this comment

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

Looks great, couple small comments.

There is no error handling or graceful teardown logic in this example. I do think that might be an important thing for users to be made aware of early on in their experimentation - equally I can see the desire to keep this lean. What do you think?

I have a question about how we intend to approach the JS/TS ecosystems with these guides - not something that needs to block this PR, but I do think we need to solve this more generally:

  • Will we have typescript equivalents?
    • More generally, how do we intend to manage JS vs TS in all code examples throughout the docs? Of course they are both AblyJS, but the usage examples will differ. Should we have a TS option in the language selector dropdown?
  • How to we handle running code snippets for JS/TS via Node vs the browser?
    • Some of the setup steps may differ, e.g. use ts-node vs use webpack vs include a script tag in an index.html, etc. How do other companies solve this problem in their guides?

* Run the following function to instantiate the SDK and establish a connection to Ably. At the minimum you need to provide an authentication mechanism. Use an API key for simplicity, but you should use token authentication in a production app. A @clientId@ ensures the client is identified, which is required to use certain features, such as presence:

```[javascript]
import * as Ably from 'ably';
Copy link
Contributor

Choose a reason for hiding this comment

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

This syntax will only work if you specify "type": "module" in the package.json, and we haven't told the reader to do that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in ce3a81d

* Install the Ably CLI: @npm install -g &#64;ably/cli@
* Run @ably login@ in the terminal and follow the prompts to provide an access token to the Ably CLI.
* Set the default app to use with @ably apps switch@ and select an API key to use for operations with @ably auth keys switch@.
* Install the Ably Pub/Sub JavaScript SDK in a new project within your IDE: @npm install ably@
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we include a step to run npm init to create a package.json before installing deps?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated prereq steps to make them clearer ce3a81d

getStarted();
```

You can monitor the lifecycle of clients' connections, but for now just log a message to the console to know that the connection attempt was successful. You'll see the message printed to your console, and you can also inspect the connection event in the dev console of your app.
Copy link
Contributor

Choose a reason for hiding this comment

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

You can monitor the lifecycle of clients' connections

How?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to explain this without going into too much detail ce3a81d


* Open up the "dev console":https://ably.com/accounts/any/apps/any/console of your first app before instantiating your client so that you can see what happens.

* Run the following function to instantiate the SDK and establish a connection to Ably. At the minimum you need to provide an authentication mechanism. Use an API key for simplicity, but you should use token authentication in a production app. A @clientId@ ensures the client is identified, which is required to use certain features, such as presence:
Copy link
Contributor

Choose a reason for hiding this comment

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

Create an index.js file with the following contents and run it with node index.js

Copy link
Contributor Author

Choose a reason for hiding this comment

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

* Join and subscribe to the presence set of the channel.
* Retrieve the messages you sent in the guide from history.

h2(#prerequisites). Prerequisites
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing Node.js installation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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


```[javascript]
const history = await channel.history();
console.log(history.items.map((message) => message.data));
Copy link
Contributor

Choose a reason for hiding this comment

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

Indentation here


```[json]
[
'Message number 1',
Copy link
Contributor

Choose a reason for hiding this comment

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

History responses are returned in reverse chronological order by default (newest first), so this would actually be 5, 4, 3, 2, 1

console.log(`Event type: ${member.action} from ${member.clientId} with the data ${JSON.stringify(member.data)}`)
});

await channel.presence.enter("I'm here!");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think using a string here but JSON in the CLI is confusing, we should just use a string in each case (I think data that says status = online is redundant, because if you're offline you won't be in the presence set, so there is not really a case where you have entered status = offline in the member data)

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 agree, however it seems the CLI currently only takes a JSON object for data. I've updated the text away from "online" whilst we fix the CLI.

@m-hulbert
Copy link
Contributor Author

Looks great, couple small comments.

There is no error handling or graceful teardown logic in this example. I do think that might be an important thing for users to be made aware of early on in their experimentation - equally I can see the desire to keep this lean. What do you think?

I have a question about how we intend to approach the JS/TS ecosystems with these guides - not something that needs to block this PR, but I do think we need to solve this more generally:

  • Will we have typescript equivalents?

    • More generally, how do we intend to manage JS vs TS in all code examples throughout the docs? Of course they are both AblyJS, but the usage examples will differ. Should we have a TS option in the language selector dropdown?
  • How to we handle running code snippets for JS/TS via Node vs the browser?

    • Some of the setup steps may differ, e.g. use ts-node vs use webpack vs include a script tag in an index.html, etc. How do other companies solve this problem in their guides?

Thanks Mike.

  • I've added a section on closing the connection with some info on unsubscribing and detaching too.
  • RE: JS vs TS and browser vs node... honestly I think we could quite easily expand on these guides with minimal effort to cover a TS version and a browser-based version.
  • In terms of TS code snippets too - I'd actually be in favour of removing the node ones we currently provide and replacing those with TS equivalents instead. What are your thoughts on that?

@m-hulbert m-hulbert requested a review from mschristensen April 24, 2025 16:37
@m-hulbert m-hulbert temporarily deployed to ably-docs-edu-1845-pubs-nrzsym April 24, 2025 16:38 Inactive
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.

4 participants