frankhale / electron-with-express

A simple app that demonstrates spawning an Express app from Electron
MIT License
662 stars 145 forks source link

Comunication between Express and Electron #12

Closed Xelarey97 closed 6 years ago

Xelarey97 commented 6 years ago

Can I communicate the electron app with the express server and get the data sended by the server?

frankhale commented 6 years ago

Yes, but what exactly do you mean? The way I use this is by using Express to serve my UI and my API and then if I need a more persistent connection I fire up a Socket.IO server and use that as well (for data or other things).

Xelarey97 commented 6 years ago

Thanks for te reply. I want to use this using Electron as main interface and then with Express make the clients can connect to the server (local) and send data to the electron app which will manage the data.

frankhale commented 6 years ago

Yes it can do this. Think of it in this way. When Electron fires up it also starts it's own personal web server that it uses. It uses this for serving up it's own UI and it's API but it can do anything you want it to do. It can work in the same tradition as a normal web server and web site but with special super powers that Electron and Node provide.

frankhale commented 6 years ago

OKay so in this example only the most basic thing is done. There are some Express routes that provide just a very basic UI. It does nothing more but the Express app could provide an API to a database. It can do anything you want it to in order to provide data. If you want something more complex you can also start up a Socket.IO server for push and pull between backend and frontend. The thing to keep in mind is that these special servers are basically started up for the sole consumption of Electron. The world is basically at your feet, you can do anything you want to do. It's fucking brilliant actually. If you need a more complex example I may be able to be bothered to create one. =)

Xelarey97 commented 6 years ago

Ohh thanks for the example, I will try to make something. If I got stuck, can I ask you for some advice? Thanks for all.

frankhale commented 6 years ago

Absolutely, if you need help please reach out! Have fun. =)

Xelarey97 commented 6 years ago

First question! How can I run a socket.io server in Electron and connect from Expres web?

frankhale commented 6 years ago

Okay, you can start a Socket.IO server in the express-app in the app.js file. Then you can start a client in the main index.html (in the root of the code repository). Of course you'd probably want to break the code out a bit cleaner than that. I put some code directly in the index.html just as a quick and dirty example but it probably shouldn't live there for long term.

If you want a more complete example I did all this in my YouTube player: https://github.com/frankhale/toby

frankhale commented 6 years ago

You still need more help?

Xelarey97 commented 6 years ago

Sorry for the late reply. So many thanks for all the help. I'm ok now. Can I ask you for help if I need It? If you want to take a look at the project It is in a public repo in my profile. The only one with js It is. Thanks!!

frankhale commented 6 years ago

Sure thing!