Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

523
Visualizações
Using Next.js with Pusher (real-time data library) - Getting error: PusherRequestError: Unexpected status code 400

I am trying to use Pusher in my Next.js application because vercel doesn't allow websockets in their serverless functions. I keep getting this error after running the program with Pusher. error - unhandledRejection: PusherRequestError: Unexpected status code 400

I made my code as basic as possible to try and get help with it. I'm just using the doc example basically:

Here is my back-end code in the Next.js serverless function:

import Channels from "pusher";

export default async function handler(req, res) {
  const channels = new Channels({
    app_id: process.env.PUSHER_APP_ID,
    key: process.env.PUSHER_APP_KEY,
    secret: process.env.PUSHER_APP_SECRET,
    cluster: process.env.PUSHER_APP_CLUSTER,
  });
  const data = req.body;

  channels && channels.trigger("my-channel", "my-event", data);

  res.end();
}

Calling this function in my client-code:

import Pusher from "pusher-js";
import { useEffect } from "react";

const TryPusher = () => {
  useEffect(() => {
    setPusherListener();
  }, []);

  const setPusherListener = () => {
    let channels = new Pusher(process.env.PUSHER_APP_KEY, {
      cluster: process.env.PUSHER_APP_CLUSTER,
    });

    let channel = channels.subscribe("my-channel");

    channel.bind("my-event", (data) => {
      console.log(data)
    });
  };

  const pushData = async (data) => {
    // /api/socket is the endpoint for my pusher function
    const res = await fetch("/api/socket", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(data),
    });

    if (!res.ok) {
      console.error("failed to push data");
    }
  }

  return (
    <button onClick={async () => await pushData({foo: bar)}>
      Get Data
    </button>
  );
};

export default TryPusher;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It might be because you are trying to access env variables from client, NextJS does not expose these to the browser. You can put NEXT_PUBLIC_ as a prefix to make it public;

Reference: https://nextjs.org/docs/basic-features/environment-variables#exposing-environment-variables-to-the-browser.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda