arboleya / electrify

Package your Meteor apps with Electron, and butter
MIT License
247 stars 52 forks source link

Communicate between Electron and meteor App #9

Closed rafaelcorreiapoli closed 9 years ago

rafaelcorreiapoli commented 9 years ago

Hello, how can I communicate between electron and meteor app? I'm trying to use ipc but I can't make it work =( Could you please help me ?

rafaelcorreiapoli commented 9 years ago

Using node_integration:true, ipc works At the same time I'm getting a lot of errors on Electron's the console: Uncaught Error: Cannot find module 'jquery' aldeed_simple-schema.js:2112 Uncaught TypeError: S is not a function

arboleya commented 9 years ago

Thats a good issue. It's on my (unshared) roadmap.

Just to keep it clear:

Meteor's server runs independently from Electron. Electron is a process which uses its own distribution of nodejs. Meteor does it as well. Meteor runs standalone as well as Electron. So they are both individual process. Electron is just used to open up the meteor's app url.

There's no open channel of comunication between Meteor and Electron.

At this point, I thought about a fast solution which consistis of adding kinda of a socket channel between meteor's server and electron code. It'll require some boilerplate for every call, like an api, but you can do this today.

Anyway, something like IPC will be ideal for this case, but I'm not sure how to proceed about linking the two processes altogether.

However the meteor's server runs in a child_process started by electron. Perhaps there can be some communication throughout the channels there are already available for this communication to happen.

Ideally, there will be a transparent bridge where you could access electron like a scope:

Meteor.startup(function(){
    Electrify.<the-whole-electron-api-here>.method([whatever], ...)
});

Makes sense? What do you think?

green-coder commented 9 years ago

Hi, I would like my client app to browse the client's local file system and access its file's content. I am a beginner with Meteor and Electron so I can be wrong, but I believe that this can only be possible if there is a way for the Meteor process to call the fs API via the Electron process. Am I correct?

A socket between the 2 processes is still the recommended way?

arboleya commented 9 years ago

@green-coder Yes, the socket is the way to go, and it'll be available in the upcoming 2.0.0 - I should release it this week.

Stay tuned.

arboleya commented 9 years ago

@green-coder Another point, not sure what you're doing but you should be able to use NodeJS fs module through Npm.require('fs') on your Meteor's server code. It's a pure NodeJS thing, and should work without any surprises.

green-coder commented 9 years ago

I am trying to write a software similar to SVN, client and server, using Meteor. My problem is on the client side (e.g. similar to Tortoise SVN) where I want to do operations like check-in / check-out / lock / unlock / watch files, as Meteor was not designed with this kind of usage for the client side.

I am counting on Electron to help me having a client which allow me native access to the file (like what Atom does with my files) but for the moment I am trapped inside Meteor's client environment ... so close to Electron's NodeJS but at the same time so far.

arboleya commented 9 years ago

Not sure if Meteor is your best bet then, perhaps you can move along with pure Electron + js/css/etc?

In the upcoming Electrify version, however, you'll be able to invoke Electron methods from Meteor, both client and server. Even though, the back-and-forth information overhead could be an obstacle, since you'll still have to code both ends -- pretty much the same way you use Meteor.methods on the server code and Meteor.call from the client code.

green-coder commented 9 years ago

From my point of view, Meteor is the best suited to my small time budget, it provides so much advantages for client-server synchronizations, and I would not be able to do this program without as I am still a beginner with web technologies.

arboleya commented 9 years ago

Fine, expanding my explanation then, some infos that could help you.

  1. There will be a Electrify.methods({...}) in the electron code (inside .electrify folder). Remember that everything you put inside this folder, will HAVE access to all Electron API, so you can define methods here that does this and that.
  2. Then you will be able to call these methods from Meteor, using Electrify.call(...), from both client and server.
  3. The bridge that glues everything is done over Socket, as mentioned. It'll be transparently and will run always asynchronously.

Finally, just one more time, perhaps an even more straightforward way is for you to simply define methods on your Meteor's server code, and call them from the client code.

Remember that the server code does have access to filesystem, as I said before, using Npm.require('fs') and so on.

Hope this can give you a clear idea of how you can accomplish what you're looking for.

green-coder commented 9 years ago

:smiley: that sounds really great.

For the 3., it would be nice to avoid binding the socket to a port, to make sure that only the 2 processes (Electron process + its child Meteor process) are using it.

I will start to write the server code and the client, and will count on your release for the last steps. Thank you a lot for your project.

arboleya commented 9 years ago

Implemented in 2.0.0 - check out the new API / usage in project README.