I am not sure I am doing this quite right. My end goal is to send a post request from a lambda function to my electron app and create a system notification. Locally I have been able to do it from post man, but when I install the app (on linux) It doesn’t work, now I am not sure where I am supposed to point my request to, in development I pointed it too. http://localhost:3000/notify what happens when you install the app. How would I send a post request to the app, eventually I want to build user accounts, so I will need to send requests to each separate user based on the lambda logic.
I am using express with electron, is there another way to handle post requests.
Here is my code so far in my main.js file
"use strict"; const { app, BrowserWindow } = require("electron"); const { Notification } = require("electron"); const express = require("express"); const bodyParser = require("body-parser"); function createWindow() { const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true, }, }); win.loadFile("index.html"); win.webContents.openDevTools(); } app.whenReady().then(createWindow); app.on("window-all-closed", () => { if (process.platform !== "darwin") { app.quit(); } }); app.on("activate", () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow(); } }); // Create a new instance of express const appE = express(); // Tell express to use the body-parser middleware and to not parse extended bodies appE.use(bodyParser.json()); // Route that receives a POST request to /sms appE.post("/notify", function (req, res) { const body = req.body; console.log(body); res.set("Content-Type", "text/plain"); function showNotification() { const notification = { title: "Basic Notification", body: `You sent: ${body.message} to Express`, }; new Notification(notification).show(); } app.whenReady().then(createWindow).then(showNotification); res.send(`You sent: ${body.message} to Express`); }); // Tell our app to listen on port 3000 appE.listen(3000, function (err) { if (err) { throw err; } console.log("Server started on port 3000"); });
Advertisement
Answer
Your approach is wrong. An HTTP server run inside your Electron app won’t generally be accessible to anything running outside of the user’s local network, so your Lambda function won’t be able to connect to it.
If you want to read notifications from a Lambda function, then you’ll need to poll it (make Ajax requests on a timer).
If you want to push messages from the server to the application, then you still need to initiate the connection from the client (so that the connection can be made outside of the user’s local network). The typical approach for this is to use Websockets. These are not Lambda function friendly though since they need to be Always On, so you would need to host a server listening for Websocket connections on some other system (like EC2). If your infrastructure is already geared up for Lambda functions then you could write one which, when triggered, sends a message to your EC2 server to trigger Websocket messages.
+-------+ +-------+ +-------+ | | HTTP | | | | | λ | Request |HTTP | Websocket |Client | | ----------------Server ------------------ | +-------+ +-------+ +-------+