I've built a real-time app using Pusher on SvelteKit, but it doesn't work when hosted with Vercel

315 views Asked by At

Although Pusher isn't originally recommended for SvelteKit, I wanted to use it, so here's a video: https://youtu.be/uT7Y_W2GYxY

When I ran it in localhost with 'npm run dev' it worked perfectly. And I hosted it with Vercel and it doesn't work.

It's not that it doesn't work at all, and it doesn't send right away when you send a request, it sends about 3 more requests and then they are sent one after another. Since I use the same Pusher server, I sent a request from localhost and received it from the hosted place as well. However, if you send it from a hosted place, the request arrives late even on localhost.

import type { RequestHandler } from "./$types";
import Pusher from 'pusher';


export const POST:RequestHandler = async ({ request }) => {
    const pusher = new Pusher({
        appId: "id",
        key: "key",
        secret: "secret",
        cluster: "ap3",
        useTLS: true
    });

    const req = await request.json();
    
    pusher.trigger("chat", "message", req);
    return new Response((JSON.stringify({"result": "success"})), {
        headers:{
            'Content-Type': 'application/json'
        }
    })
}

This is the code I wrote in +server.ts. Receive json and send to pusher server.

It's strange that there is a delay in the request. Vercel seems to recommend using Pusher when building real-time apps, so why is this happening?

If you need additional materials please ask. can show

0

There are 0 answers