N
Neon2y ago
robust-apricot

websockets fail on nodejs test package

Hey all, I have a monorepo which looks something like this
mono/
├─ apps/
│ └─ web
└─ packages/
├─ db
└─ test
mono/
├─ apps/
│ └─ web
└─ packages/
├─ db
└─ test
My db works fine running from the dev server for the 'web' app, but if I run from the test package I get the following
Error: All attempts to open a WebSocket to connect to the database failed. Please refer to https://github.com/neondatabase/serverless/blob/main/CONFIG.md#websocketconstructor-typeof-websocket--undefined. Details: fetch failed
Error: All attempts to open a WebSocket to connect to the database failed. Please refer to https://github.com/neondatabase/serverless/blob/main/CONFIG.md#websocketconstructor-typeof-websocket--undefined. Details: fetch failed
I'm in the middle of migrating the project from pscale/mysql so everything was working before. I've tried installing wsas a dependency for the test package but still no luck. My db package looks like this (using drizzle)
import { drizzle } from 'drizzle-orm/neon-serverless';
import { Pool } from '@neondatabase/serverless';
import dotenv from "dotenv";
import * as schema from "./schema";

dotenv.config({ path: ".env.local" });

const pool = new Pool({ connectionString: process.env.DB_URL, });

export const db = drizzle(pool, { schema })
import { drizzle } from 'drizzle-orm/neon-serverless';
import { Pool } from '@neondatabase/serverless';
import dotenv from "dotenv";
import * as schema from "./schema";

dotenv.config({ path: ".env.local" });

const pool = new Pool({ connectionString: process.env.DB_URL, });

export const db = drizzle(pool, { schema })
test/package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test:e2e": "NODE_ENV=development playwright test",
},
"keywords": [],
"author": "",
"license": "ISC",
"private": true,
"devDependencies": {
"@playwright/test": "^1.39.0",
"@types/node": "^20.10.4"
},
"dependencies": {
"@faker-js/faker": "^8.1.0",
"db": "workspace:*",
"dotenv": "^16.3.1",
"ws": "^8.16.0"
}
}
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test:e2e": "NODE_ENV=development playwright test",
},
"keywords": [],
"author": "",
"license": "ISC",
"private": true,
"devDependencies": {
"@playwright/test": "^1.39.0",
"@types/node": "^20.10.4"
},
"dependencies": {
"@faker-js/faker": "^8.1.0",
"db": "workspace:*",
"dotenv": "^16.3.1",
"ws": "^8.16.0"
}
}
Since it's working on the web app, I'm not sure what else to do 🤷‍♂️
2 Replies
robust-apricot
robust-apricotOP2y ago
The error sends me here: https://github.com/neondatabase/serverless/blob/main/CONFIG.md#websocketconstructor-typeof-websocket--undefined
Set this parameter if you're using the driver in an environment where the WebSocket global is not defined, such as Node.js, and you need transaction or session support.

For example:

import { neonConfig } from '@neondatabase/serverless';
import ws from 'ws';
neonConfig.webSocketConstructor = ws;
Set this parameter if you're using the driver in an environment where the WebSocket global is not defined, such as Node.js, and you need transaction or session support.

For example:

import { neonConfig } from '@neondatabase/serverless';
import ws from 'ws';
neonConfig.webSocketConstructor = ws;
But I'm using the Pool constructor for the drizzle connection which doesnt accept a webSocket
GitHub
serverless/CONFIG.md at main · neondatabase/serverless
Connect to Neon PostgreSQL from serverless/worker/edge functions - neondatabase/serverless
robust-apricot
robust-apricotOP2y ago
Fixed it! 🥴 should of just followed the instructions:
import { drizzle } from 'drizzle-orm/neon-serverless';
import { Pool, neonConfig } from '@neondatabase/serverless';
import dotenv from "dotenv";
import * as schema from "./schema";
import ws from 'ws';

dotenv.config({ path: ".env.local" });
neonConfig.webSocketConstructor = ws;
const pool = new Pool({ connectionString: process.env.DB_URL });

export const db = drizzle(pool, { schema })
import { drizzle } from 'drizzle-orm/neon-serverless';
import { Pool, neonConfig } from '@neondatabase/serverless';
import dotenv from "dotenv";
import * as schema from "./schema";
import ws from 'ws';

dotenv.config({ path: ".env.local" });
neonConfig.webSocketConstructor = ws;
const pool = new Pool({ connectionString: process.env.DB_URL });

export const db = drizzle(pool, { schema })
The Singleton pattern for the neonConfig threw me through a loop

Did you find this page helpful?