N
Neon2y ago
absent-sapphire

How to use Neon WSProxy with Prisma and Vercel Edge Functions.

I'm currently having trouble Neon's WS Proxy (Here) with Prisma, especially how I should fill out the variables and what URIs I should put in. My .env currently:
DATABASE_URL="postgresql://coolperson:<private>@example.com:1234/postgres"
DATABASE_URL="postgresql://coolperson:<private>@example.com:1234/postgres"
My db.ts:
import { PrismaClient } from '@prisma/client';
import { neonConfig, Pool } from '@neondatabase/serverless';
import { PrismaNeon } from '@prisma/adapter-neon';

// Copied from https://github.com/prisma/prisma/issues/21310#issuecomment-2023676550
if (global.WebSocket === undefined) {
const ws = require('ws')
neonConfig.webSocketConstructor = ws
}

let databaseUrl = process.env.DATABASE_URL;

if (databaseUrl.includes('localhost')) {
neonConfig.wsProxy = () => `wsproxy.example.com:3456/v1`
neonConfig.useSecureWebSocket = false
neonConfig.pipelineConnect = false
}

const pool = new Pool({ connectionString: databaseUrl })
const adapter = new PrismaNeon(pool)
const prisma = new PrismaClient({
adapter,
datasources: {
db: { url: 'wsproxy.example.com:3456' },
},
})

export default prisma;
import { PrismaClient } from '@prisma/client';
import { neonConfig, Pool } from '@neondatabase/serverless';
import { PrismaNeon } from '@prisma/adapter-neon';

// Copied from https://github.com/prisma/prisma/issues/21310#issuecomment-2023676550
if (global.WebSocket === undefined) {
const ws = require('ws')
neonConfig.webSocketConstructor = ws
}

let databaseUrl = process.env.DATABASE_URL;

if (databaseUrl.includes('localhost')) {
neonConfig.wsProxy = () => `wsproxy.example.com:3456/v1`
neonConfig.useSecureWebSocket = false
neonConfig.pipelineConnect = false
}

const pool = new Pool({ connectionString: databaseUrl })
const adapter = new PrismaNeon(pool)
const prisma = new PrismaClient({
adapter,
datasources: {
db: { url: 'wsproxy.example.com:3456' },
},
})

export default prisma;
There are errors but the errors just come from how the libraries cannot connect to the database.
2 Replies
extended-salmon
extended-salmon2y ago
Can you clarify your question? Are you trying to connect Neon to a Postgres running locally? If so, here's a working example: https://github.com/evanshortiss/neon-docker-wss-proxy-local
GitHub
GitHub - evanshortiss/neon-docker-wss-proxy-local
Contribute to evanshortiss/neon-docker-wss-proxy-local development by creating an account on GitHub.
absent-sapphire
absent-sapphireOP2y ago
Thank you this was actually what I needed, I saw the repo on Google but never actually looked into it. Is there a Next.JS and Prisma example? nevermind

Did you find this page helpful?