Pusher socket_id and channel_name Undefined in Next.js API Route

I’m working on a Next.js app and trying to integrate Pusher for real-time updates with authentication. However, I’m having trouble accessing the socket_id and channel_name in my API route; they both appear as undefined.

I’ve tried retrieving these values from both the query parameters and the request body, but they are always undefined. I’ve searched for examples online but mostly found examples using the old pages directory, not the new app directory in Next.js.

Here’s my code:

pusherClient.ts:

"use client";
import Pusher from "pusher-js";
import { createClient } from "@/utils/supabase/client";

const supabase = createClient();
const session = supabase.auth.getSession();

const pusher = new Pusher(process.env.NEXT_PUBLIC_PUSHER_KEY ?? '', {
    cluster: process.env.NEXT_PUBLIC_PUSHER_CLUSTER ?? '',
    authEndpoint: "/api/pusher/auth",
    auth: {
        headers: {
            'Authorization': `Bearer ${session?.then(session => session.data.session?.access_token)}`
        }
    },
});

export default pusher;

app/api/pusher/auth/route.ts:

import { NextRequest, NextResponse } from "next/server";
import Pusher from "pusher";
import { createClient } from "@/utils/supabase/server";

const pusher = new Pusher({
    appId: process.env.PUSHER_APP_ID!,
    key: process.env.PUSHER_KEY!,
    secret: process.env.PUSHER_SECRET!,
    cluster: process.env.PUSHER_CLUSTER!,
    useTLS: true,
});

export async function POST(req: NextRequest) {
    console.log("Handler invoked");
    try {
        const url = new URL(req.url);
        console.log(url.searchParams.forEach((value, key) => console.log(`${key}: ${value}`)));
        const socketId = url.searchParams.get('socket_id');
        const channelName = url.searchParams.get('channel_name');

        console.log("Socket ID:", socketId);
        console.log("Channel Name:", channelName);

        const contentType = req.headers.get('content-type') || '';
        if (!contentType.includes('application/json')) {
            return NextResponse.json({ error: 'Invalid content type' }, { status: 400 });
        }

        const { socket_id, channel_name } = await req.json();
        console.log("Socket ID:", socket_id);
        console.log("Channel Name:", channel_name);

        const authorizationHeader = req.headers.get('authorization');
        const token = authorizationHeader?.split(' ')[1];

        if (!token) {
            console.error("Missing auth token");
            return NextResponse.json({ error: "Missing auth token" }, { status: 401 });
        }

        console.log("Token:", token);

        const supabase = createClient();
        const { data: { user }, error } = await supabase.auth.getUser(token);

        if (error || !user) {
            console.error("Invalid or expired token", error);
            return NextResponse.json({ error: "Invalid or expired token" }, { status: 401 });
        }

        const userId = user.id;
        console.log("User ID:", userId);

        if (channel_name === `private-user-${userId}`) {
            const auth = pusher.authorizeChannel(socket_id, channel_name);
            return NextResponse.json(auth);
        } else {
            console.error("Forbidden: User not authorized for channel", channel_name);
            return NextResponse.json({ error: "Forbidden" }, { status: 403 });
        }
    } catch (error) {
        console.error("Internal Server Error", error);
        return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
    }
}

pusher.ts (server side)

"use server";
import Pusher from "pusher";

const pusher = new Pusher({
    appId: process.env.PUSHER_APP_ID ?? '',
    key: process.env.PUSHER_KEY ?? '',
    secret: process.env.PUSHER_SECRET ?? '',
    cluster: process.env.PUSHER_CLUSTER ?? '',
    useTLS: true,
});

export default pusher;

Fixed, request body is in application/x-www-form-urlencoded format rather than application/json

 export async function POST(req: NextRequest) {
    console.log("Handler invoked");
    try {
        // Get socket_id and channel_name from body form
        let socket_id, channel_name;

        if (req.headers.get('content-type') === 'application/json') {
            // Parse JSON body
            const body = await req.json();
            socket_id = body.socket_id;
            channel_name = body.channel_name;
        } else if (req.headers.get('content-type') === 'application/x-www-form-urlencoded') {
            // Parse URL-encoded body
            const body = await req.text();
            const params = new URLSearchParams(body);
            socket_id = params.get('socket_id');
            channel_name = params.get('channel_name');
        } else {
            return NextResponse.json({ error: 'Unsupported content type' }, { status: 415 });
        }

        console.log("Socket ID:", socket_id);
        console.log("Channel Name:", channel_name);

        return NextResponse.json({ auth: pusher.authorizeChannel(socket_id, channel_name) });

    } catch (error) {
        console.error("Internal Server Error", error);
        return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật