← Topics

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.

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

Step 1: the server client

Keep one client in a server-only module:

// 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, 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:

// 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

'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 wraps the client in a provider with hooks for channels, events and presence, and works in the App Router. The Next.js use case builds a live page end to end, and the Node.js guide 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.

Product names are trademarks of their owners.