@vv-01/web-push-service
v1.0.0
Published
Web push notification service for NestJS - but web-only
Downloads
91
Maintainers
Readme
🚀 Web Push Service
A complete, production-ready Web Push Notification service for NestJS applications. OneSignal-like capabilities, but web-only and open-source.
npm package: @vv-01/web-push-service
Features
✅ Web-Only Push Notifications - Uses browser Push API + Service Workers ✅ NestJS Integration - Drop-in module, zero config (after VAPID keys) ✅ TypeScript Support - Full type safety ✅ Multi-tenant Ready - App-scoped subscriptions ✅ User Tracking - Optional user ID tracking ✅ Custom Metadata - Attach any data to subscriptions ✅ Automatic Cleanup - Handles expired subscriptions (410 Gone) ✅ npm Package - Publish as reusable module ✅ Frontend SDK - Minimal JavaScript library ✅ Service Worker Example - Copy-paste ready
Project setup
npm installGenerate VAPID Keys
npx web-push generate-vapid-keysStore these in your .env:
VAPID_PUBLIC_KEY=YOUR_PUBLIC_KEY
VAPID_PRIVATE_KEY=YOUR_PRIVATE_KEYBackend Setup
1. Configure NestJS
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { WebPushModule } from './push/push.module';
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'sqlite',
database: 'push.db',
synchronize: true,
autoLoadEntities: true,
}),
WebPushModule.register({
vapidPublicKey: process.env.VAPID_PUBLIC_KEY,
vapidPrivateKey: process.env.VAPID_PRIVATE_KEY,
vapidEmail: 'mailto:[email protected]',
}),
],
})
export class AppModule {}2. Compile and run
# development
npm run start:dev
# production
npm run start:prodFrontend Setup
1. Copy Service Worker
Copy sdk/service-worker.example.js to public/sw.js
2. Install SDK
npm install @vv-01/web-push-service3. Use in React/Vue/etc
import WebPushSDK from '@vv-01/web-push-service/sdk/web-push-sdk';
const sdk = new WebPushSDK({
serverUrl: 'http://localhost:3000',
userId: 'user-123',
});
await sdk.init();
await sdk.subscribe();API Endpoints
| Method | Endpoint | Description |
| ------ | ----------------------------- | ----------------------- |
| GET | /push/key | Get VAPID public key |
| POST | /push/subscribe | Save subscription |
| DELETE | /push/unsubscribe/:endpoint | Remove subscription |
| POST | /push/send-all | Send to all subscribers |
| POST | /push/send-user/:userId | Send to specific user |
| GET | /push/stats/:appId | Get subscription count |
Database Schema
CREATE TABLE push_subscriptions (
id INT PRIMARY KEY AUTO_INCREMENT,
endpoint TEXT UNIQUE NOT NULL,
p256dh TEXT NOT NULL,
auth TEXT NOT NULL,
userAgent VARCHAR(500),
userId VARCHAR(255),
appId VARCHAR(255) DEFAULT 'default',
metadata JSON,
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);Run tests
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:covDeployment
When you're ready to deploy your NestJS application to production, there are some key steps you can take to ensure it runs as efficiently as possible. Check out the deployment documentation for more information.
If you are looking for a cloud-based platform to deploy your NestJS application, check out Mau, our official platform for deploying NestJS applications on AWS. Mau makes deployment straightforward and fast, requiring just a few simple steps:
$ npm install -g @nestjs/mau
$ mau deployWith Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure.
Resources
Check out a few resources that may come in handy when working with NestJS:
- Visit the NestJS Documentation to learn more about the framework.
- For questions and support, please visit our Discord channel.
- To dive deeper and get more hands-on experience, check out our official video courses.
- Deploy your application to AWS with the help of NestJS Mau in just a few clicks.
- Visualize your application graph and interact with the NestJS application in real-time using NestJS Devtools.
- Need help with your project (part-time to full-time)? Check out our official enterprise support.
- To stay in the loop and get updates, follow us on X and LinkedIn.
- Looking for a job, or have a job to offer? Check out our official Jobs board.
Support
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
Stay in touch
- Author - Kamil Myśliwiec
- Website - https://nestjs.com
- Twitter - @nestframework
License
Nest is MIT licensed.
