question.. web push notifications having trouble with modules

Hi. I’m new to Deno. Overall I’ve been happy with is performance and how lightweight and secure it is. The place I’m having trouble is building a successful server with modules. For example, most of the imports I need to use aren’t available anymore. Here’s an example of the code I can’t get modules to import with: import { Application, Router } from 'https://deno.land/x/oak/mod.ts'; import { oakCors } from "https://deno.land/x/cors/mod.ts"; import { json } from "https://deno.land/std/http/server.ts"; import { Application, Router, send } from "https://deno.land/x/oak/mod.ts"; import * as path from "https://deno.land/std/path/mod.ts"; import { webpush } from "https://cdn.skypack.dev/web-push"; import { TextDecoder } from "https://deno.land/std/encoding/utf8.ts"; import { crypto } from "https://deno.land/std/node/crypto.ts"; import { renderFileToString } from "https://deno.land/x/dejs/mod.ts"; import { v4 as uuidv4 } from "https://deno.land/std/uuid/mod.ts"; import { Client } from "https://deno.land/x/mysql/mod.ts"; const app = new Application(); const router = new Router(); const port = 3000; // Database const client = new Client(); await client.connect({ hostname: "localhost", port: 3600, db: "mysql", tls: false, }); const db = client.database("subscriptions"); const subscriptionCollection = db.collection("subscription"); // Middlewares app.use(oakCors()); app.use(json()); // Static files app.use(async (ctx) => { await ctx.send({ root: ${Deno.cwd()}/views, index: "index.html", }); }); // Service Worker Notifications // Generate your own vapid keys by running: // deno run --allow-write --allow-read https://deno.land/x/web_push/cli.ts const publicVapidKey = "<<Public Key>>"; const privateVapidKey = "<<Private Key>>"; webpush.setVapidDetails( "mailto:mail@someEmail.com", publicVapidKey, privateVapidKey ); Here’s the remaining part: // Subscribe router.post("/subscribe", async (ctx) => { try { const body = await ctx.request.body(); const subscription = body.value; const hashValue = crypto.subtle.digest("SHA-256", new TextEncoder().encode(JSON.stringify(subscription))); const hash = Array.from(new Uint8Array(hashValue)).map(byte => byte.toString(16).padStart(2, '0')).join(''); const checkSubscription = await subscriptionCollection.findOne({ hash: hash, }); let theMessage = JSON.stringify({ title: "You have already subscribed", body: "Although we appreciate you trying to again", }); if (!checkSubscription) { const newSubscription = { hash: hash, subscriptionEl: subscription, }; await subscriptionCollection.insertOne(newSubscription); theMessage = JSON.stringify({ title: "Thank you for Subscribing!", body: "Some body message here", }); } ctx.response.status = 201; ctx.response.body = {}; await webpush.sendNotification(subscription, theMessage, { vapidDetails: webpushVapidDetails }); } catch (e) { console.log(e); } }); router.get("/sendwebpushnotification", async (ctx) => { try { const allSubscriptions = await subscriptionCollection.find(); for (const item of allSubscriptions) { let theMessage = JSON.stringify({ title: "Some notification", body: "Some description", }); await webpush.sendNotification(item.subscriptionEl, theMessage, { vapidDetails: webpushVapidDetails }).catch( (error) => { console.error(error.stack); } ); } ctx.response.status = 200; ctx.response.body = { message: "notification sent" }; } catch (e) { console.log(e); } }); app.use(router.routes()); app.use(router.allowedMethods()); router.use("/", router.routes()); app.use(router.routes()); app.use(router.allowedMethods()); await app.listen({ port: 80 }); const mail = "random@mydomain"; const publicKey = "vapidpub"; const privateKey = "vapidpriv"; // (E) SEND TEST PUSH NOTIFICATION router.get("/", async (ctx) => { await send(ctx, "/testme.html", { root: Deno.cwd() }); }); router.post("/goodboy", async (ctx) => { ctx.response.status = 201; ctx.response.body = {}; try { await webpush.sendNotification(ctx.request.body, JSON.stringify({ title: "😊", body: "👍🏼", icon: "i-ico.png", image: "i-banner.png" }), { vapidDetails: webpushVapidDetails }); } catch (err) { console.log(err); } }); // (F) START! app.listen(port, () => console.log(🙏🏽 is live on port ${port}));
There's a couple of problems. - you are importing oak Application and Router twice * To avoid issues with changing modules you should use explicit versions in import urls. You can keep using old deno.land/std modules as long as you specify a version * I don't think you need to use skypack anymore, use deno's built-in node compat layer instead by importing with "npm:web-push" * Not sure why textdecoder from std is needed since it's a built in api that's usable without importing anything * Same for crypto
npm:web-push have a problem with crypto lib. I can't use it on my project, instead I create another service on Vercel with standard node app to only send notifications with web-push lib. See this comment: https://github.com/denoland/deno/issues/20851#issuecomment-1765501127
☂️ Node/npm compatibility issue for Q4 2023 · Issue #20851 · denola...
This is an umbrella issue for Node.js/npm compatibility work that we will undertake in Q4 2023. We made a lot of progress in Q2 with polyfills for node:http, node:https and node:http2 getting more ...
Thank you I’m going to try that. I’m still learning Deno and coming from php it’s been a bit of a curve but one that I’m loving and happy to be moving to. I’m working on each area of a production level system so we are 100% Deno and increase performance. Dam that was a long 18 + hours trying to make my code function. Just when I was so close to finishing it there were just so many failures and the web push library from NPM I converted to Deno wouldn’t work without the crypto module.

Looking for more? Join the community!

Recommended Posts
Certain code prevents Deno from doing anythingAny time I try and use Deno with `npm:zeromq@6.0.0-beta.19`, running my code has absolutely no outpuCustom `401 Unauthorized` error page, similar to builtin `404 Not Found`?How can I create a custom error page that is rendered by middleware in Fresh? The middleware handlesUsing `deno_emit` from Rust: building import graphI'm trying to use `deno_emit` from Rust to bundle modules. I already evaluated these modules with `dNPM Packages Usage on deno_core ?Hello, I'm visiting from https://deno.com/blog/roll-your-own-javascript-runtime-pt2. Utilizing JSRun`deno.disablePaths` doesn't workHi in `.vscode\settings.json` I have this: ```json { "deno.disablePaths": ["**/kit", "./kit", ".What is the correct syntax to set the `compilerOptions.types` to use npm @types libraryFor example: As of now if I want my .ts files to have access to the `npm:express` packages `@types/Using "imports" in deno.json doesn't work in VS.code* deno 1.38.3 * vs.code 1.84.2 * denoland.vscode-deno 3.28.0 I got rid of my import map and moved tdeno repl start time in containerWhy does deno repl take a couple seconds to start in a container the second time around, and starts How to detect when --watch flag has been passed to `deno run` programatically?I am writing a small cli script, and I have a few bits of code that do not play nicely with `deno ruHelp with npm peerDependenciesHi 👋 I am using an npm library that has `@sinclair/typebox` listed under `peerDependencies`. It giAWS Lambda configHow do i set permissions in deno for aws lambda. Like --allow-env etcMigrating from EventEmitter…Hi! I've got some legacy code that depends on `node:events` 's `EventEmitter` class, specifically itusing flexsearchso flexsearch has a index.d.ts thats broken and hasn't been touched in 2ish years and instead you shDoes Deno third party modules support to wrap NPM in importmapHere is my first module: https://deno.land/x/lume_fluent@1.0.0-alpha My importmap for the module isHelp using DenoMaileri'm trying to use it but i get Deno is not defined when i try to send the email, any tips? how wouldDebug all tests in VSCodeHi everyone. I was hoping to use the equivalent of "deno test" in such a way that I can set a breakMost complete Deno Rust runtime?I’m trying to embed the Deno runtime into my Rust program. I’m looking for the most “complete” Deno Language server crashing in VSC outside of deno projectsHow to write a new project template scriptHey, I've written a small game engine to create text adventures in Deno. The resulting project is a Seamlessly include TailwindcssIs there a recommended way of achieving this at the moment? What is the cleneast way of including ta