jlongster / electron-with-server-example Goto Github PK
View Code? Open in Web Editor NEWAn example Electron app with a backend server all wired up via IPC
An example Electron app with a backend server all wired up via IPC
after cloning, renaming to lightroom-catalog-helper
, and running npm install
, running npm start
on Windows 10 x64 leads to the following error:
C:\Users\Pomax\Documents\Git\projects\lightroom-catalog-helper>npm start
> [email protected] start C:\Users\Pomax\Documents\Git\projects\lightroom-catalog-helper
> electron .
checking 1
Service path not specified, so defaulting to ipc.config.socketRoot + ipc.config.appspace + id
requested connection to myapp1 /tmp/app.myapp1
Connecting client on Unix Socket : /tmp/app.myapp1
######
error: { Error: connect ENOENT \\.\pipe\tmp-app.myapp1
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1061:14)
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'connect',
address: '\\\\.\\pipe\\tmp-app.myapp1' }
found socket 1
connection closed myapp1 /tmp/app.myapp1 Infinity tries remaining of Infinity
Yuki exceeded connection rety amount of or stopRetrying flag set.
(Yuki being the machine name for this particular Win10 installation)
Most people use webpack but it adds a lot of confusion to this simple demo. An example based on webpack make things clearer
In theory any process on the machine could communicate with the socket right? How have you secured this with your app?
Electron 12 it will be enabled contextIsolation by default.
https://www.electronjs.org/docs/tutorial/context-isolation
contextBridge.exposeInMainWorld('electron', {
getSocketId: () => socketPromise,
ipcConnect: (id, func) => {
ipc.config.silent = true
ipc.connectTo(id, () => {
func(ipc.of[id])
})
},
...
})
const connectSocket = (socketId: string) => {
window.electron.ipcConnect(socketId, (client) => {
client.on('message', (data: string) => {
const msg = JSON.parse(data)
...
I create a local web server and use http requests to communicate with UI.
Is it advisable?
Thank you :)
First of all, thanks for the awesome example. It's really helpful.
Can a TL;DR section be added? It will be helpful for people who have already read through the entire readme file and can use it for quick reference\lookup
something like this:
File | description |
---|---|
index.js | The main renderer process |
server-ipc.js | IPC handler for the server |
Hi,
It's 2023, and I'm reading electron docs about IPC. I'm in doubt how the ideas from this repo (and the blog related to it) compares to native implementation of electron IPC. Is it possible to implement these ideas using electron IPC? Is it still worth to do that ?
Thanks!
Ely
Hi James,
I've used this repository and your articles about using IPC communication with electron with much success--they've been super helpful!
I just wanted to post a heads up about the node-ipc
package: sadly the author sabotaged it in March of 2022 by adding malware aimed at machines with Russian IP addresses. see: malware article
I'll be looking for alternatives for node-ipc now.... If you have any recommendations I'd appreciate it!
Thanks,
Bill.
Is it possible and recommended to use cluster in childprocess (ipc-server.js) to improve server performance with more than 1 cpu?
try the following implementation according to the ipc-node documentation:
`const ipc = require('node-ipc')
const fs = require('fs')
const cpuCount = require('os').cpus().length
const cluster = require('cluster')
const socketPath = '/tmp/ipc.sock'
function init(socketName, handlers) {
ipc.config.unlink = false
if (cluster.isMaster) {
if (fs.existsSync(socketPath)) {
fs.unlinkSync(socketPath)
}
for (let i = 0; i < cpuCount; i++) {
cluster.fork()
}
} else {
ipc.serve(socketPath, () => {
ipc.server.on('message', (data, socket) => {
let msg = JSON.parse(data)
let { id, name, args } = msg
if (handlers[name]) {
handlers[name](args).then(
(result) => {
ipc.server.emit(socket, 'message', JSON.stringify({ type: 'reply', id, result }))
},
(error) => {
// Up to you how to handle errors, if you want to forward
// them, etc
ipc.server.emit(socket, 'message', JSON.stringify({ type: 'error', id }))
throw error
}
)
} else {
console.warn('Unknown method: ' + name)
ipc.server.emit(socket, 'message', JSON.stringify({ type: 'reply', id, result: null }))
}
})
})
ipc.server.start()
console.log(`pid ${process.pid} listening on ${socketPath}`);
}
}
function send(name, args) {
ipc.server.broadcast('message', JSON.stringify({ type: 'push', name, args }))
}
module.exports = { init, send }`
But I get an error:
TypeError: handle.setSimultaneousAccepts is not a function at ChildProcess.target._send (internal/child_process.js:761:16) at ChildProcess.target.send (internal/child_process.js:676:19) at sendHelper (internal/cluster/utils.js:22:15) at send (internal/cluster/master.js:351:10) at internal/cluster/master.js:317:5 at SharedHandle.add (internal/cluster/shared_handle.js:29:3) at queryServer (internal/cluster/master.js:311:10) at Worker.onmessage (internal/cluster/master.js:246:5) at ChildProcess.onInternalMessage (internal/cluster/utils.js:43:8) at ChildProcess.emit (events.js:215:7)
Any help is welcome thanks.
Electron的server只能运行的C盘吗
我把app装在D盘 发现服务的运行目录在C盘
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.