Search code examples
phpelectrondesktop-application

Electron desktop app with online server and database?


I am working on a desktop application with electron and I am considering online storage to store data. I would like to get some idea on the approach as I couldn't find reliable answers from google search.

Approach 1. electron app (front end ) + php (like purchase a hosting package from godaddy with a domain e.g: www.mysite.com)

with this approach I am planning to create api calls in php to perform basic CRUD.

is this is a good way? will this affect the speed/load time?

are there better ways for this situation? Thank you very much in advance for the help.


Solution

  • Well, this is not an easy topic. Your solution could work: you Electron app ask your server for data and store data to it. Anyway the best solution depends from your application.

    The most important points that you have to ask yourself are:

    • How often do you need to reach your server ?
    • Your users could work without data from server ?
    • How long does it takes to read and store data on your server ? (it's different if you store some kb or many gb of data)
    • The data stored online must be shared with other users or every user has access to its own data ?

    If all the information are stored in your server your startup have to wait for the request to complete but you can show a loader or something like this to mitigate the waiting.

    In my opinion you have many choices, from the simplest (and slowest) to the most complex (but that mitigate network lag):

    1. Simple AJAX requests to your server: as you described you will do some HTTP requests to your server and read and write data to be displayed on your application. Your user will have to wait for the requests to complete. Show them some loading animations to mitigate the wait.
    2. There are some solutions that save the data locally to your electron installation and then sync them online, Have a check to PuchDB for an example
    3. Recently I'm looking at GraphQL. GraphQL is an API to query your data. It's not that easy but it has some interesting features, it has an internal cache and it's already studied for optimistic update. You update your application immediately thinking that your POST will be fine and then if something goes wrong you update it accordingly.

    I'd also like to suggest you to try some solutions as a service. You don't have a server already and you will have to open a new contract so why don't you check some dedicated service like Firebase? Google Firebase Realtime Database allows you to work in javascript (just one language involved in the project), sync your data online automatically and between devices without the need to write any webservice. I'have just played with it for some prototypes but it looks very interesting and it's cheap. It also has a free plan that it's enough for many users.

    Keep in mind that if your user has access only to their data the fastest and easies solution is to use a database inside your electron application. A sqlite database, an IndexDB database or even serialize to JSON and then store everything in localstorage (if your data fits size limits).

    Hope this helps