DEV Community

KoichiArai
KoichiArai

Posted on

Day12~17 of 100DaysOfCode

I have been struggling with Inter-Process Communication (IPC) for 5 days. (Also dealing with COVID too)
Finally, I have understood how to use methods such as 'ipcRenderer.invoke' and 'ipcMain.handle' a bit.
However, there are still many things to learn.

What I did

I couldn't understand event.reply even after using chat-GPT. I referred to the following article and used .invoke and .handle instead.

And here is what I made:

// main.js
console.log("Main process working");
console.log("main.js");
"use strict"
const { app, BrowserWindow, ipcMain, dialog} = require('electron');
const path = require('path');
const url = require('url');
let win;

function createWindow() {
    const mainWindow = new BrowserWindow({
        width: 400,
        height: 300,
        title: 'IPC App',
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
        }
    });
    ipcMain.handle('open-dialog', async (_e, _arg) => {
        return "hogehoge"
            // .showOpenDialog(mainWindow, {
            //     properties: ['openFile'],
            // })
            // .then((result) => {
            //     if (result.cancelled) return "Cancelled hoge";
            //     return "hoge";
            // });
    });
    mainWindow.loadFile('index.html');
};


app.once('ready', () => {
    createWindow();
});

app.once('window-all-closed', () => app.quit());
Enter fullscreen mode Exit fullscreen mode
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Security-policy" content="default-src 'self'" />
    <meta name="viewport" content="width=device-width, inicial-scale=1.0" />
    <title>IPC App</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <h2>Input something</h2>
    URL: <input id="URL"/>
    <button id="errorBtn">Open</button>
    <p id="text"></p>
    <script src="index.js"></script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
const {ipcRenderer} = require("electron");

const Button = document.getElementById("Button");
const text = document.getElementById("text");

errorBtn.addEventListener('click', async () => {
    console.log("index.js");
    text.textContent = await ipcRenderer.invoke('open-dialog');
});
Enter fullscreen mode Exit fullscreen mode

I will try to use the packaging function tomorrow.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❀ or a friendly comment on this post if you found it helpful!

Okay