AnyCable Real-time Starter Kit

R
Renaudil y a 2 jours
0

Description

AnyCable/Next.js chat application demonstrating how to build real-time features, with all benefits of open-source. No PaaS bills.

npx boilerapp anycable

文档

AnyCable Next.js/Vercel Example

Deploy with Vercel

This is an example of how to use AnyCable with Next.js and Vercel to build real-time applications.

Learn more about AnyCable for serverless JavaScript apps in the documentation.

Prerequisites

You will need to deploy an AnyCable-Go server to the platform of your choice. We recommend using Fly.io and provide an example configuration in fly.toml.example for seamless deployments. However, Anycable-Go server can be deployed on any platform.

Using fly CLI, run the following command to create and launch a new AnyCable-Go application:

# Create a new Fly application
fly launch --image anycable/anycable-go:1.4 --generate-name --ha=false --internal-port 8080 --env PORT=8080 --env ANYCABLE_BROKER=memory

Answer "No" to all database-related questions and "Yes" to deployment. This will deploy your app and create a fly.toml file with the minimum configuration. See the fly.toml.example file to learn more about other available and recommended configuration options.

Deployment

  • Click the Deploy button

  • Fill in the required environment variables:

    CABLE_URL=wss://<YOUR_ANYCABLE_GO_HOSTNAME>/cable
    ANYCABLE_HTTP_BROADCAST_URL=https://<YOUR_ANYCABLE_GO_HOSTNAME>/_broadcast
    ANYCABLE_HTTP_BROADCAST_SECRET=<YOUR_SECRET>
    ANYCABLE_JWT_ID_KEY=<YOUR_JWT_SECRET>
    
    • The ANYCABLE_HTTP_BROADCAST_SECRET and ANYCABLE_JWT_ID_KEY can be any strings.
    • You can create a secure value using this CLI command openssl rand -hex 32
  • Set the following environment variables on your AnyCable-Go server:

    ANYCABLE_RPC_HOST=https://<YOUR_VERCEL_APP_HOSTNAME>/api/anycable
    ANYCABLE_HTTP_BROADCAST_SECRET=<YOUR_SECRET>
    ANYCABLE_JWT_ID_KEY=<YOUR_JWT_SECRET>
    

When using Fly, you can keep all env vars in the .env.production file and import the secrets using the following command:

cat .env.production | fly secrets import

When deploying to Vercel you can use the Vercel CLI to pull environment variables:

vercel env pull

Authentication

We use the AnyCable JWT identification feature to issue JWT tokens to authenticate clients. The benefit of using AnyCable JWTs is the ability to verify and identify clients at the WebSocket server side without making additional requests to the backend (Vercel functions in our case).

The ANYCABLE_JWT_ID_KEY environment variable is responsible for this.

Running locally

PNPM is required to install dependencies.

First, install the dependencies:

pnpm install

Then, start AnyCable-Go:

pnpm anycable-go

And start the Next.js app:

pnpm dev

Open http://localhost:3000 with your browser to see the result.

Prix

Gratuit

FREE

评论 (0)

常见问题

常见问题解答 (FAQ)

有问题?我们有答案。如果您找不到想要的答案,请随时联络我们。

Boilerapp 是一个专门用于分享 Boilerplates、入门套件(Starter Kits)和项目模版的开发者社区平台。我们的目标很简单:为您节省初始配置(Setup)的时间,让您可以专注于真正重要的代码。无论您是在寻找简单的代码库还是完整的 SaaS 项目,都能在这里找到。

还有其他问题?

我们的团队随时为您提供帮助。联络我们,我们将尽快回复。