rsnext/examples/with-ably
Karl Horky 3ad55721d1
Remove incorrect entries for pnpm debug log (#47241)
**Reasons for making this change:**

- it is contained within `node_modules/`, which is already ignored
- the previous versions, which were not in `node_modules/`, did not have
a period at the beginning of the filename

Links to documentation supporting these rule changes:

**Changelog with proof here:**


ba4b2db1f2/pnpm/CHANGELOG.md (L3330)

History:

- my PR to remove this from `github/gitignore` here:
https://github.com/github/gitignore/pull/4250
- First introduced in `github/gitignore` in
https://github.com/github/gitignore/pull/3732 by `@sakurayang` (merged
by `@martinwoodward`)

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation or adding/fixing Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md



## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

---------

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2023-03-26 22:26:05 -07:00
..
pages Adding with-ably example for realtime messaging in next apps (#37319) 2022-06-29 12:52:04 +02:00
public Adding with-ably example for realtime messaging in next apps (#37319) 2022-06-29 12:52:04 +02:00
styles Adding with-ably example for realtime messaging in next apps (#37319) 2022-06-29 12:52:04 +02:00
.gitignore Remove incorrect entries for pnpm debug log (#47241) 2023-03-26 22:26:05 -07:00
next-env.d.ts Remove incorrect entries for pnpm debug log (#47241) 2023-03-26 22:26:05 -07:00
package.json Adding with-ably example for realtime messaging in next apps (#37319) 2022-06-29 12:52:04 +02:00
README.md docs(examples): improve DX while copying command to create new project (#38410) 2022-07-26 21:57:48 -05:00
tsconfig.json Adding with-ably example for realtime messaging in next apps (#37319) 2022-06-29 12:52:04 +02:00
types.d.ts Adding with-ably example for realtime messaging in next apps (#37319) 2022-06-29 12:52:04 +02:00

Realtime Edge Messaging with Ably

Demo: https://next-and-ably.vercel.app/

Add realtime data and interactive multi-user experiences to your Next.js apps with Ably, without the infrastructure overhead.

Use Ably in your Next.js application using idiomatic, easy to use hooks.

Using this demo you can:

This demo is uses the Ably React Hooks package, a simplified syntax for interacting with Ably, which manages the lifecycle of the Ably SDK instances for you taking care to subscribe and unsubscribe to channels and events when your components re-render).

Deploy your own

Deploy the example using Vercel or preview live with StackBlitz

You will need an Ably API key to run this demo, see below for details

Deploy with Vercel

How to use

Execute create-next-app with npm, Yarn, or pnpm to bootstrap the example:

npx create-next-app --example with-ably with-ably-app
yarn create next-app --example with-ably with-ably-app
pnpm create next-app --example with-ably with-ably-app

Deploy it to the cloud with Vercel (Documentation).

When deployed, ensure that you set your environment variables (the Ably API key and the deployed Vercel API root) in your Vercel settings

Notes

Ably Setup

In order to send and receive messages you will need an Ably API key. If you are not already signed up, you can sign up now for a free Ably account. Once you have an Ably account:

  1. Log into your app dashboard.
  2. Under “Your apps”, click on “Manage app” for any app you wish to use for this tutorial, or create a new one with the “Create New App” button.
  3. Click on the “API Keys” tab.
  4. Copy the secret “API Key” value from your Root key.
  5. Create a .env file in the root of the demo repository
  6. Paste the API key into your new env file, along with a env variable for your localhost:
ABLY_API_KEY=your-ably-api-key:goes-here
API_ROOT=http://localhost:3000

How it Works/Using Ably

Configuration

pages/_app.js is where the Ably SDK is configured:

import { configureAbly } from '@ably-labs/react-hooks'

const prefix = process.env.API_ROOT || ''
const clientId =
  Math.random().toString(36).substring(2, 15) +
  Math.random().toString(36).substring(2, 15)

configureAbly({
  authUrl: `${prefix}/api/createTokenRequest?clientId=${clientId}`,
  clientId: clientId,
})

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}

export default MyApp

configureAbly matches the method signature of the Ably SDK - and requires either a string or a AblyClientOptions object. You can use this configuration object to setup your tokenAuthentication. If you want to use the usePresence function, you'll need to explicitly provide a clientId.

You can do this anywhere in your code before the rest of the library is used.

useChannel (Publishing and Subscribing to Messages)

The useChannel hook lets you subscribe to a channel and receive messages from it:

import { useState } from 'react'
import { useChannel } from '@ably-labs/react-hooks'

export default function Home() {
  const [channel] = useChannel('your-channel', async (message) => {
    console.log('Received Ably message', message)
  })
}

Every time a message is sent to your-channel it will be logged to the console. You can do whatever you need to with those messages.

Publishing a message

The channel instance returned by useChannel can be used to send messages to the channel. It is a regular Ably JavaScript SDK channel instance.

channel.publish('test-message', { text: 'message text' })

usePresence

The usePresence hook lets you subscribe to presence events on a channel - this will allow you to get notified when a user joins or leaves the channel. The presence data is automatically updated and your component re-rendered when presence changes:

import { useState } from 'react'
import { usePresence } from '@ably-labs/react-hooks'

export default function Home() {
  const [presenceData, updateStatus] = usePresence('your-channel-name')

  const presentClients = presenceData.map((msg, index) => (
    <li key={index}>
      {msg.clientId}: {msg.data}
    </li>
  ))

  return <ul>{presentClients}</ul>
}

You can read more about the hooks available with the Ably Hooks package on the @ably-labs/ably-hooks documentation on npm.