Prisma pulse issue
whne trying to connect pulse got this error, I also tried .stream() method, same result....
I'm using nextjs 15 with server actions
Error creating stream: [TypeError: fetch failed] {
[cause]: Error [InvalidArgumentError]: invalid upgrade header
at new Promise (<anonymous>) {
code: 'UND_ERR_INVALID_ARG'
}
}
PULSE FUNCTION
'use server';
import { prisma } from '../prisma-client';
export const getBookingStream = async () => {
try {
const stream = await prisma.booking.subscribe();
console.log('Stream created successfully', stream);
} catch (error) {
console.error('Error creating stream:', error);
}
};
CLIENT INIT
import { PrismaClient } from '@prisma/client/edge';
import { withAccelerate } from '@prisma/extension-accelerate';
import { withPulse } from '@prisma/extension-pulse/workerd';
const globalForPrisma = globalThis as unknown as { prisma: ReturnType<typeof createPrismaClient>; };
function createPrismaClient() {
return new PrismaClient().$extends(withAccelerate()).$extends(
withPulse({
apiKey: process.env.PULSE_API_KEY as string,
}),
);
}
export const prisma = globalForPrisma.prisma || createPrismaClient();
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma;
6 Replies
You opted for speed from this bot. Let's conjure up an answer right now!
Based on the information provided in the knowledge sources and your question, it appears you're encountering an issue with Prisma Pulse. Let's address a few points:
1. Environment: You're using Prisma Pulse in a serverless or edge environment, as indicated by your import of
PrismaClient
from '@prisma/client/edge' and the use of the withAccelerate
extension.
2. Error: The error you're encountering suggests there's an issue with the network connection or the way the request is being made to the Pulse service.
3. Usage: You're trying to use both .subscribe()
and .stream()
methods, but encountering the same error.
Here are some potential solutions and things to check:
1. Prisma Pulse is server-side only: According to the Known limitations documentation, "Prisma Pulse streams cannot be initiated directly within client-side code. To receive events in real-time, Prisma Client needs to open a stream on a long-running server."
This means that you can't use Pulse directly in edge functions or serverless environments that don't maintain long-lived connections.
2. Use the correct import: For Cloudflare Workers (which seems to be your environment based on the '/workerd' import), make sure you're using the correct import as mentioned in the Getting started with Prisma Pulse documentation:
You're already doing this correctly.
3. Check your Pulse API key: Ensure that your PULSE_API_KEY
environment variable is set correctly and that the key is valid.
4. Use .stream()
instead of .subscribe()
: The .stream()
method is recommended over .subscribe()
as mentioned in the API reference:
5. Ensure you're using compatible versions: Make sure you're using Prisma Client version 4.16.1 or higher and @prisma/extension-pulse version 1.1.0 or higher.
If you've checked all these points and are still encountering issues, it's possible that there's a limitation with using Prisma Pulse in edge or serverless environments that isn't explicitly documented. In this case, you might need to consider using Pulse in a long-running server environment instead.
For further assistance, you may want to reach out to Prisma support or post your question on the Prisma community Discord channel.
š¤ Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly.
In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.Hey š
Can you try connecting to Pulse in a long running server instead od server actions and check if you get the same error?
When using Server actions, Vercel deploys backend code via serverless functions which are unable to maintain a steady WebSocket connection to a frontend.
Vercel's network does not support inbound WebSocket connections (frontend to serverless function) and serverless functions are limits to a max of 60 seconds (I think). These two hurdles make it difficult to use Pulse with Server Actions.
You meant I should use
@prisma/extension-pulse not @prisma/extension-pulse/workerd?
@Nurul (Prisma)
if so, currently cannot do that, do you have plans to fix it? or I should find something else?
No, i meant using a long running server if possible. As you are using Nextjs server actions, it internally deploys your code on serverless functions
which causes this issue
okay, I see, thanks!
No worries š