{"id":1315,"date":"2021-10-27T12:33:50","date_gmt":"2021-10-27T10:33:50","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=1315"},"modified":"2021-11-03T14:37:18","modified_gmt":"2021-11-03T13:37:18","slug":"how-to-integrate-electron-ipcrenderer-into-angular-app-based-on-typescript","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2021\/10\/27\/how-to-integrate-electron-ipcrenderer-into-angular-app-based-on-typescript\/","title":{"rendered":"How to integrate Electron ipcRenderer into Angular app based on TypeScript?"},"content":{"rendered":"\n<p>Let\u00b4s say we want to use&nbsp;<a href=\"https:\/\/github.com\/atom\/electron\/blob\/master\/docs\/api\/ipc-main.md\">ipcMain<\/a>&nbsp;\/&nbsp;<a href=\"https:\/\/github.com\/atom\/electron\/blob\/master\/docs\/api\/ipc-renderer.md\">ipcRenderer<\/a>&nbsp;on our project to communicate from Angular to Electron and back.<\/p>\n\n\n\n<p>The Electron side look like this: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const { ipcMain } = require('electron');\n\nipcMain.on('asynchronous-message', function(event, arg) {\n  console.debug('ipc.async', arg);\n  event.sender.send('asynchronous-reply', 'async-pong');\n});\n\nipcMain.on('synchronous-message', function(event, arg) {\n  console.debug('ipc.sync', arg);\n  event.returnValue = 'sync-pong';\n});<\/code><\/pre>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-red-color\">how to integrate the &#8220;ipcRenderer&#8221; Electron module into our Renderer ( Here Angular app) ?<\/span><\/h4>\n\n\n\n<h4><em>Solutions:<\/em><\/h4>\n\n\n\n<p><strong><span class=\"has-inline-color has-vivid-green-cyan-color\">Robust way<\/span><\/strong>. Register object&nbsp;<code>require<\/code>&nbsp;returned:<\/p>\n\n\n\n<p>if for example , we are using&nbsp;<a href=\"https:\/\/github.com\/systemjs\/systemjs\">SystemJS<\/a>&nbsp;as module loader<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script&gt;\n    System.set('electron', System.newModule(require('electron')));\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>This is the best, because&nbsp;<code>renderer\/init.js<\/code>&nbsp;script loads that module on start. SystemJS have to take it only, not loads.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong><span class=\"has-inline-color has-vivid-green-cyan-color\">Alternative way<\/span><\/strong>. Use dirty trick with declaration.<\/p>\n\n\n\n<p>Get electron instance inside&nbsp;<code>index.html<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script&gt;\n      const { ipcRenderer } = require(\"electron\");\n&lt;\/script&gt;\n<\/code><\/pre>\n\n\n\n<p>Declare it inside your&nbsp;<code>typescript<\/code>&nbsp;file this way:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>declare var ipcRenderer: any;\n<\/code><\/pre>\n\n\n\n<p>Use it with freedom : )<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ipcRenderer.send(...)<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let\u00b4s say we want to use&nbsp;ipcMain&nbsp;\/&nbsp;ipcRenderer&nbsp;on our project to communicate from Angular to Electron and back. The Electron side look like this: how to integrate the &#8220;ipcRenderer&#8221; Electron module into our Renderer ( Here Angular app) ? Solutions: Robust way. Register object&nbsp;require&nbsp;returned: if for example , we are using&nbsp;SystemJS&nbsp;as module loader This is the best, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1338,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[37,364,5,83],"tags":[362,363],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1315"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1315"}],"version-history":[{"count":4,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1315\/revisions"}],"predecessor-version":[{"id":1339,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1315\/revisions\/1339"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1338"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}