# Migrate Next.js from Pusher: route handlers, auth route and pusher-js

> Move a Next.js app off Pusher Channels: the server client’s host, the App Router auth route, pusher-js in client components, preview deployments and webhooks.

Source: https://hibira.io/topics/migrate-from-pusher-nextjs
Updated: 2026-11-18

A Next.js app on Pusher Channels usually has three pieces: the `pusher` package in route handlers or server actions, an auth route for private channels, and pusher-js in client components. All three keep their code when you move to a Pusher-compatible service. You change the host in two places and the environment variables behind them.

## What changes and what stays

| Part of your app | Changes? | What to do |
| --- | --- | --- |
| `pusher.trigger()` in route handlers and server actions | No | Add the `host` option once |
| The auth route with `authorizeChannel()` | No | Nothing; the signatures are the same |
| pusher-js in client components | Yes | Add `wsHost` and the new key |
| `.env.local` and your host's environment settings | Yes | New credentials and host |
| Webhook route | No | Add the URL on the new app |
| Allowed origins | New | List your domains, including preview URLs; the Free plan needs exact origins |

## Step 1: the server client

Keep one client in a server-only module:

```ts
// lib/pusher-server.ts
import 'server-only';
import Pusher from 'pusher';

export const pusher = new Pusher({
  appId: process.env.PUSHER_APP_ID!,
  key: process.env.NEXT_PUBLIC_PUSHER_KEY!,
  secret: process.env.PUSHER_APP_SECRET!,
  host: process.env.NEXT_PUBLIC_PUSHER_HOST ?? 'ws.hibira.io',
  useTLS: true,
});
```

Route handlers and server actions call `pusher.trigger('orders', 'order-shipped', { id })` as before. The `host` option is in the [pusher-http-node README](https://github.com/pusher/pusher-http-node), checked on 2026-11-18.

## Step 2: the auth route

pusher-js posts `socket_id` and `channel_name` as a form. In the App Router:

```ts
// app/api/pusher/auth/route.ts
import { pusher } from '@/lib/pusher-server';
import { auth } from '@/auth';

export async function POST(req: Request) {
  const session = await auth();
  if (!session?.user) return new Response('Forbidden', { status: 403 });
  const form = await req.formData();
  const socketId = String(form.get('socket_id'));
  const channel = String(form.get('channel_name'));
  if (channel.startsWith('presence-')) {
    return Response.json(pusher.authorizeChannel(socketId, channel, { user_id: session.user.id, user_info: { name: session.user.name } }));
  }
  return Response.json(pusher.authorizeChannel(socketId, channel));
}
```

## Step 3: the browser client

```ts
'use client';
import Pusher from 'pusher-js';

export const pusherClient = new Pusher(process.env.NEXT_PUBLIC_PUSHER_KEY!, {
  cluster: 'mt1',
  wsHost: process.env.NEXT_PUBLIC_PUSHER_HOST,
  wsPort: 443,
  wssPort: 443,
  forceTLS: true,
  enabledTransports: ['ws', 'wss'],
  channelAuthorization: { endpoint: '/api/pusher/auth', transport: 'ajax' },
});
```

`NEXT_PUBLIC_` values are inlined at build time, so redeploy after changing them. Create the client once at module level, not in a component body, or each render opens another connection.

## Step 4: switch without a gap

Tabs that are already open keep their Pusher connection until they reload. For a day or two, call `trigger()` on both a Pusher client and the new one, then remove the old one when Pusher's dashboard shows no more connections. For webhooks, read the raw body with `await req.text()` and pass it with the headers to `pusher.webhook({ headers, rawBody })`; `isValid()` checks the same `X-Pusher-Signature`.

## Hooks instead of wiring

[@hibira-io/react](https://hibira.io/guides/react) wraps the client in a provider with hooks for channels, events and presence, and works in the App Router. The [Next.js use case](https://hibira.io/use-cases/nextjs) builds a live page end to end, and the [Node.js guide](https://hibira.io/guides/node) covers the server library.

## Questions

### Does it work on Vercel and other serverless hosts?

Yes. Publishing is one HTTPS request from the server library, so it fits route handlers, server actions and serverless functions. The WebSocket connections are held by the service, not by your functions.

### Why do preview deployments fail to connect?

Each preview has its own URL. On the Free plan, add each origin you use to the app's allowed origins; paid plans can allow any origin with `*`.

### Do I need a separate app for development?

It is a good idea. Use one app for local development and previews and another for production, so test events never reach real users.

### Can server components subscribe to channels?

No. Subscribing needs a browser WebSocket, so it happens in client components. Server components and server actions publish.
