Struggling on IPC communications


#1

Hello guys,
I’m new on electron and i’m working on sendin messages app.quit(); methodes. Is there any problem in my code because there is nothing appear after I click the button ?

Thanks alot.
main.js

const electron = require('electron');
const {app} = electron;
const {BrowserWindow} = electron;
const {ipcMain} = require('electron');
let win;
function createWindow() {
    win = new BrowserWindow({width: 800, height: 600 ,frame: false});
    win.loadURL(`file://${__dirname}/index.html`);
    win.on('closed', () => {
        win = null;
        
});
}
app.on('ready', createWindow);
app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
    app.quit();
}
});
app.on('activate', () => {
    if (win === null) {
    createWindow();
}
});
ipcMain.on('messageOnipc', function (){
         console.log('Message On ipc');
         app.quit();
});

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script type="javascript" src="js/bootstrap-min.js"></script>
    <script type="javascript" src="js/npm.js"></script>
    <link type="text/css" rel="stylesheet" href="css/bootstrap.min.css">
    <script>
        const {ipcRenderer} = require('electron');
        document.getElementById('btn-close').addEventListener('click',function (){
            ipcRenderer.send('messageOnipc');
        });
    </script>
</head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="#" >WriteLN</a>
            </div>
        </div>
    </nav>
    <div class="container">
        <div class="col-md-1">
            <h1>Hello World</h1>
            <button class="btn-danger" id="btn-close" type="button" >Close</button>
        </div>
    </div>
</body>
</html>

#2

Have you had any luck with IPC?
I am working with an Ionic2 app and have tried ipcRenderer but not working for me yet.
Thanks.


#3

I moved your “script” tag from “head” to the end of HTML, just before “/body” and it worked.

Reason: the script, because it’s in “head”, is referring to an element that does not exist yet.


#4

Gauz09, did you have any luck with Ionic2 and ipc? I replied to you in that forum.
When importing or requiring ipc ( main or renderer ) I only get this error:
fs.readFileSync is not a function

Thanks.


#5

Hi Shepard, thanks for the reply in other forum. I will check it out. However, I got it working. :slight_smile:
Now working on creating an installer for that app for all different OS.

As for Ionic2 and ipc, I didn’t have the need to do it in my case. I was able to embed all of my Ionic pages in Electron and get it to work. I have tried it with regular HTML and JS and was able to implement it. That is, send events/messages from HTML/JS Renderer to Main using IPC.

Maybe, I will try it with Ionic2 once I figure out about the installer part.


#6

Hi Gauz09, ya I was able to get the regular html, like on this thread, to work and close the app. Thanks for the proof that it can work at all!

If you ever do get IPC on Ionic2 to talk - I am totally interested.
Likewise, if I get lucky, I’ll return here.

I’m surprised at the lack of anyone else doing this. Or mentioning their success/failures at it.
Good luck!