Push Notifications Ionic - node.js

First of all, I'm developing a proyect in Ionic (front-end), Node.js + Express (backend) and MongoDB as DB and, I want to implement push notification in my app. I've read a lot about notification using firebase but I'm using MongoDB. I'm quite lost if anyone could help/guide me, I'd appreciate it.

You can use Firebase push or Firebase cloud messaging with your app without any issue. It is just one service of Firebase family. You don't need to use its real-time database with your app. You just need to integrate Firebase cloud messaging service only. i.e. you can keep Mongodb backend as it is now.
Article about Firebase Push and Ionic 4

If you don't want to use firebase and/or you want push notifications even if you are offline, there is this Cordova plugin you can use: cordova-plugin-local-notifications.
Quick instructions here. Not-so-quick instructions: read the documentations and issues in github.
Install like this:
ionic cordova plugin add cordova-plugin-local-notification
npm install #ionic-native/local-notifications
Then import and include the provider in app.module.ts (don't forget this step, usually not included in any manual):
import {LocalNotifications} from '#ionic-native/local-notifications/ngx';
...
providers: [ LocalNotifications ]
Then import and inject in your_page.page.ts:
import {LocalNotifications} from '#ionic-native/local-notifications/ngx';
...
constructor(private localNotifications: LocalNotifications) {}
And finally use it like this:
this.localNotifications.schedule({
title: 'My first notification',
text: 'Thats pretty easy...',
foreground: true
});
Test it thoroughly, the plugin is not bugfree, but works.
Hope this helps.

Related

How to add server-side database access to Nuxt app on Fly.io

I am trying to get started with Fly.io. I know Vue well and wanted to try Nuxt and Node.js. I cannot seem to figure out how to add server-side components and classes for handling AJAX requests.
I followed the official tutorial https://fly.io/docs/languages-and-frameworks/nuxtjs/
npm init nuxt-app#latest spec-land
create-nuxt-app v5.0.0
✨ Generating Nuxt.js project in spec-land
? Project name: spec-land
? Programming language: JavaScript
? Package manager: Npm
? UI framework: Element
? Template engine: HTML
? Nuxt.js modules: Axios - Promise based HTTP client
? Linting tools: (Press <space> to select, <a> to toggle all, <i> to invert selection)
? Testing framework: None
? Rendering mode: Single Page App
? Deployment target: Server (Node.js hosting)
? Development tools: jsconfig.json (Recommended for VS Code if you're not using typescript)
? What is your GitHub username? --
? Version control system: Git
🎉 Successfully created project spec-land
$ touch pages/index.js
$ npm run dev
After adding some front-end code, is this generated project equipped to run JavaScript on the server in the same fly.io deploy?
For a Nuxt2 app, you need to have Rendering Mode as Universal (SSR/SSG) or ssr: true in your nuxt.config.js file, otherwise, you will not have an isomorphic app.
Nuxt will run some code on the server side + client side (isomorphic), while some will be run only on the client side only.
The setup to have a serverMiddleware for Nuxt2 is as follows. Quite tricky and not working so well.
Nuxt3 handles that pretty well on the opposite side.
Overall, Nuxt2 does have some capabilities regarding server-side code but is not as flexible as Nuxt3. Tbh, if you want to use Nuxt2, I would recommend not trying to set up an actual database linked to it, but rather accessing it remotely via axios or like.

Creating a main account twilio client on a subaccount node function

I'm developing a twilio function on a subaccount that needs to acces a twilio client on the main account. The function is being deployed through a Github action to the subaccount.
For this, on the subaccount function I have:
const mainClient = new twilio(context.MAIN_TWILIO_ACCOUNT_SID, context.MAIN_TWILIO_AUTH_TOKEN);
Then I need to access serverless.services but I'm getting serverless undefined therefore can't access services.
i.e.
return mainClient.serverless.services(...
I tested code locally and it worked but failed on deployment. Is it possible to access services on main account from a subaccount function??
Thanks.
For anyone experiencing a similar issue.
My problem was that deploying without being explicit about dependencies doesnt install the latests version of dependencies.
Deploy through github installed twilio node sdk version 3.29.0 but this version doesn includes serverless api yet. So I explicitly added latest twilio node sdk (3.75.0 at the moment) and that fixed the problem.
Thanks.

NuxtJS & Firebase : upgrading to NodeJS 16 engine breaks Firestore listener (Firebase rules)

I've been using NuxtJS (v2.15.8) with Nuxt Firebase (v7.6.1), running on NodeJS engine 12 (v12.21.0 to be exact) for the web application I've been developping incrementally for the past couple of years and my web app is now quite complex.
I am trying to upgrade NodeJS to the latest LTS version (v16.13.2) and encounter one major issue after switching version of NodeJS (using nvm) and changing the package.json of my five packages from node 12 to node 16 :
package.json :
"engines": {
"node": "16",
..
},
When running exactly the wame web application after these changes, it starts correctly but Firebase Rules seem to break, with this error FirebaseError: false for 'get' # L61, false for 'get' # L268.
It is a cryptic error, but from experience and from all I could find online, it happens when a call to Firestore that gets blocked by defined Firebase Security rules). In my case, it happens on a "onSnapshot" call to listen to the changes of the currently logged in user. Some other calls to Firestore (using "get" and not "onSnapshot") seem to work fine, and the Firebase Authentication works well too.
Here is the full error stack :
loggedInUser.js?384a:65 Error listening to user changes
FirebaseError: false for 'get' # L61, false for 'get' # L268
at new n (prebuilt-306f43d8-45d6f0b9.js?23bd:188:1)
at eval (prebuilt-306f43d8-45d6f0b9.js?23bd:10426:1)
at eval (prebuilt-306f43d8-45d6f0b9.js?23bd:10427:1)
at n.onMessage (prebuilt-306f43d8-45d6f0b9.js?23bd:10449:1)
at eval (prebuilt-306f43d8-45d6f0b9.js?23bd:10366:1)
at eval (prebuilt-306f43d8-45d6f0b9.js?23bd:10397:1)
at eval (prebuilt-306f43d8-45d6f0b9.js?23bd:15160:1)
at eval (prebuilt-306f43d8-45d6f0b9.js?23bd:15218:1)
The portion of code triggerring the error is :
listenUser({ commit }, userId) {
const userRef = this.$fire.firestore.collection('users').doc(userId);
userListener = userRef.onSnapshot(function(userDoc) {
if (userDoc.exists) {
const user = userConverter.fromFirestoreData(userDoc.data());
commit('SET_LOGGED_IN_USER', user);
}
},
function(error) {
console.error("Error listening to user changes", error);
});
},
As soon as I revert back to Node 12, the same call works fine and isn't blocked by the Firebase rules, so the error doesn't appear.
I therefore have several questions :
Does anyone understand what's happening there ? Is there known changes in the behavior of Firebase rules directly related to the NodeJS engine ?
Do you think this issue can come from Nuxt or its Nuxt Firebase module are not working correctly under NodeJS 16 ?
It is required to also upgrade NuxtJS to a newer version or should it be possible to simply update the Node Engine ?
Is it required to update to a newer version of Firebase (modular implementation) despite the Nuxt Firebase module stating :
"This module does not support the new modular syntax from Firebase v9+. If you plan to use the new modular mode of Version 9, we advise you to implement Firebase manually as described in the following medium article. It is currently unclear when, and if, this module will support the new modular mode."
Source : their Github repo
Any help to understand what's going on here is welcome !!
Thanks a lot for your help !
Regarding your questions:
I'm unaware of what is causing this issue but there are no known changes in the behavior of Firebase Rules depending on the NodeJS version you are using.
It's hard to assess without having more information. However I deployed a sample NuxtJS app following this guide on NodeJS 16 and it worked. Additionally the error code, as you mentioned, is caused when a Firestore Rule blocks a query. Therefore I think the root cause might be in the NuxtJS firebase module.
I wasn't able to find any documentation suggesting that you need to upgrade NuxtJS when upgrading NodeJS. Additionally you mentioned that you are using version 2.15.8 of NuxtJS which according to this release notes is the latest version.
I'm unsure on further support for NuxtJS considering that statement, but according to this Firebase documentation it is recommended to upgrade to version 9.
If you decide to attempt to upgrade to firebase v9 make sure to also upgrade Nuxt Firebase module to version 8.0.0 or higher, this version provides support to the compat library so you can use Firebase v9 although still with the old syntax, more information can be found here.
Lastly, if you'd like to test if a Firebase rule is working as expected you can quickly test it using the Rules Playground.
Long story short : upgrading to Firebase v9 worked.
Before I did that, I got stuck with rules preventing me to access firestore documents as soon as I tried running the project under Node16 engine.
So I had to do the following changes :
updating Firebase to v9
implement the configuration through a plugin rather than the nuxt-firebase module
make all the required changes in my code to make use of v9 modular (I didn't try using the compat version)
Now that I use the latest version of Firebase, I tried again switching to NodeJS 16 and it runs fine, including the Firebase security rules.

How to expose a module from an Electron app to an external module

I am creating an Electron app and am using electron-builder to package and build the app. Users are able to make plugins for the app (plugins can be node modules with their own dependencies and such). Everything is working fine except the part of the app for exposing the app's API to the plugin.
I created a module in the app for handling the plugins "Plugin-handler" that imports the plugin and also exposes the API to the plugin (The app's API is just a set of functions and it is bundled with the app).
The dilemma is that the user should be able to place a plugin anywhere on their machine and the app does not know the path before the build. Consequently, I excluded the "plugin-handler" module in the Electron-builder config so it does not bundle with the Webpack. Now I need to find the right way to expose the API to the plugin.
Here is how I am doing it now, to load the plugins and passing the API:
// In the Plugin-handler module
const API = require('api')
const plugin = require('path-to-plugin')( API )
path-to-plugin is added by the user in the app when they import their plugin.
As seen above, currently I pass the API to the plugin as an argument, which is not ideal, instead, I need a way to exposing the API module (or any other module that is bundled in the APP) to the plugin so users can access it in their plugin like below:
// In the plugin
const { arg1, arg2,... } = require('api')
I've seen apps doing this, and allowing users to access their API in their plugins, but since I am new to all this, I may be doing things wrong, so please do be kind, and thank you for the help!
I drew a simple chart for better portraying the question:

How to build chatbot using botkit anywhere in nodejs application?

I am trying to build a chatbot using botkit anywhere and nodejs. I don't want to use any third-party messaging platform. I refer to this link https://github.com/howdyai/botkit#build-your-bot and try to setup chatbot in nodejs application:
First, add it to your project:
npm install --save botkit
Then, add Botkit to your application code:
var Botkit = require('botkit');
var controller = Botkit.anywhere(configuration);
controller.hears('hello','direct_message', function(bot, message) {
bot.reply(message,'Hello yourself!'); });
But they don't mention how to call and where to call this code in the existing application.
Before you start you need a NLU middle ware. Since you don't want to use 3rd party services, you can use RASA NLU and Botkit has easy integration too.
https://github.com/RasaHQ/rasa_nlu
Then You can the below easy to use Botkit-Anywhere-RASA library.
https://github.com/matteoredaelli/botkit-starter-web-rasa-nlu
1) Star the RASA NLU server
2) Clone the starter project (#2) and run using "node ."
For Botkit Web anywhere:
I followed following URL steps:
https://botkit.ai/getstarted.html
after these steps my folder structure is like below:
you can navigate to public->client.js and here you can find method.

Resources