Best way to handle a freezing thread in electron js - javascript

I am working on a project in electronjs with vue cli.
I am making some api calls that by their nature freeze the thread they're in. The API is archaic, and I need to use https://www.npmjs.com/package/winax this npm package to handle the response and process it to xml/json/etc.
The fact that the app is freezing when I call the api is already a horrible thing and I needed to find a viable way to have those calls running in the background. I have tried worker_threads, and vanilla web workers but I couldn't require this crucial winax package so my last resort solution was to create a new BrowserWindow that loads a html file that has my API calling class in its script.
The method works as I'm hiding the window that freezes but this solution comes with a lot of problems as I am not able to export that class and call it somewhere else. Because of this I am required to use the remote module, set global variables in order to handle the communication between my API responses and the other windows in my app.
I would happily include any other details that I am allowed to, however I am quite restricted by the confidentiality of my project so I'll do my best.
My question is: does this seem like something you've encountered and could point me in the right direction on how to handle those troublesome API calls?
Electron version: 8.5.5
OS: Windows 10 Pro
The app is intended to work only on Windows
I do not have any technical nodejs/electron knowledge in my company - in case I get an answer telling me to go to my senior :D

Related

To what extent does Node.js replace Apache2?

I'm in the process of updating an old piece of software (running Apache2, PHP, MariaDB, HTML, CSS) to use a React frontend. I'm completely new to React and I'm between beginner and intermediate with vanilla JavaScript so I've been researching this the last few days.
My confusion is about backend with a React web app. If I want to keep the existing architecture in place aside from frontend, will I be able to painlessly have my apache2/php integrate with my React app? And I'm also confused about the role of Node.js. Some places describe it as just extra libraries, and some places describe it as an Apache2 replacement. I think the source of my confusion is that so far in learning React and Node it always requires me to run npm start. Which feels very much like running a server like apache.
I guess in a nutshell my question is - what will my project structure look like when using a React frontend? Will I have just javascript files and host that on Apache? Or will this need to be run on Node? And where does my PHP fit in, will this need re-written in JS?
EDIT : For further clarification. I'm asking about the differences between a Node.js server and Apache2 server. That's the focus of my issue. Additional points of discussion to help me understand the problem are how PHP and React fit into all this, and whether my understanding of Node as an Apache-like tool is correct. Hopefully that makes this question less broad
For the purposes of building a React app against an existing set of web services: Node.js is used only at build time. It is used to execute the build tools which generate static files which are deployed on your Apache HTTP server.
Node.js is also used to run the development server for the React application that hot reloads it as you save changes to the source code.
Node.js can be used to run an HTTP server which hosts web services written in JavaScript (which which case it either replaces Apache or sits behind Apache (which is configured to act as a reverse proxy) … but that isn't your use case.

ElectronJS - Cache HTML and JS files from remote server

I have an electron app that retrieves the app files (.html & .js) from a remote server using the function mainWindow.loadURL('http://www.example.com/index.html')
The problem arises if the users network connection to the internet is offline or disconnected.
Is there a way in electron to cache the html and js files so that if the user is offline, electron will automatically load from the cache.
I have tried to use the HTML5 Application Cache and a plugin for webpack https://github.com/NekR/offline-plugin but these do not seem to work.
I see this is an old question but I stumbled across this when doing a semi-related search and there is no answer at all right now, so I'll provide one:
Ignoring the Electron-specific nature of this question, the web-standard way to do this is using Service Workers. Here are some docs on that:
"Using Service Workers" from MDN - https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers - this is a reference source.
"Adding a Service Worker and Offline into your Web App" - https://developers.google.com/web/fundamentals/codelabs/offline/ - this is a tutorial.
"Creating Offline-First Web Apps with Service Workers" - https://auth0.com/blog/creating-offline-first-web-apps-with-service-workers/ - this is also a tutorial.
I think this would be the most direct way to solve this, even within Electron. (An advantage of Electron here is that you have a single, known browser to make this work for, but I think what you are trying to do fits perfectly within the problem-space that Service Workers are designed to address.)
That said, I think Sayam's comment/question is valid -- if this html/js is the actual content of your electron app, and assuming it doesn't change too often you could (and maybe should) distribute it with the app itself. Then you don't need to do anything special for offline support (as long as that html/js doesn't need network-based resources), and changes to that code are deployed as application updates.
Personally I think that once-per-week is about the maximum frequency of updates for which this approach is suitable. It would not bother me if an app auto-updated 2 or 3 times per month, but I think I'd uninstall an app that updates itself 2 or 3 times per week if I had that option.
There may also be some electron and/or node modules that address this problem-space, but I've never bothered to look because one of the two options above has always seemed appropriate to me.
Old question but still valid usecase (offline cache for dynamic assets).
here is article that describes one solution for that (own ExpressJS caching middleware). Author made npm library to address that.

Web scraping in react-native ios?

I am building an iOS app with react-native and looking to do some web scraping. From my understanding traditional npm packages don't work because they rely on Node.js. I know there are Objective-C/Swift libraries for web scraping but I don't understand how I would integrate those into a react-native component.
Anyone have any ideas on how to get information from a website (web scraping) in react-native?
Interesting question. Architecturally, it might be a good idea to consider building a service which can provide the scraped data via an API. The more processing power the client requires (the heavier the client-side code), the more likely you are to run into lagging / stuttering issues in the app. Phones have come a long way, but still can't match a server's power.
If you want to press ahead, I would advise using the browserified version of Cheerio.js. Basically, browserify lets you take code written for node, and use it in a browser environment. You would need to test it in JS Core on IOS, but there's a good chance everything will work.
One other option is using jQuery's load function or the fetch api included in React Native to make the call to the site (you don't have to worry about CORS), and parse the result manually.

Windows Store App: Dynamically load JavaScript for easier update?

Recently I have taken over a Windows Store app project written in C#/XAML and one of its requirements is,
Migrating the project to Javascript/HTML5, and by making javascript dynamically loaded from our website, we can update the code logic as often as desired without having to prompt user to download a new version of our app. The deployed app is very simple and does not require update, each time the app launch it will try to load the javascripts and contents from the web.
This requirement is from marketing and they think it is fantastic if the app can be updated in this way. I don't know if this is a good idea, or even feasible.
My questions
Give me some reasons if this is not a good idea/not feasible
If javascript can be dynamically loaded, what about html and css files?
Edit
When I asked this question, I had not heard of Cordova, the idea of hybrid app was quite new at that time. With Cordova, we can write app that runs in this way. The web content (HTML/JS/CSS) is rendered in a WebView control and can be updated from the web each time.
In short, I don't believe the app can be written in this way.
The application code is part of the appx package when the app is deployed and if you wish it to be accessible via the store, it has to pass MS's app certification checks.
This ensures that the app code is is suitable quality and ticks the appropriate security boxes etc.
I don't believe you can dynamically load application code from a remote source, otherwise you could be loading any old unverified cack and this code would not have been checked.
However, the updating mechanisms are very simple as I understand it and pushing a new version of your app code to the store will allow users to update the app at their leisure and handle the process for you.
I think the best you can get in terms of loading things dynamically would be to load a webpage remotely in your app (the webpage could be updated as you wanted). This would not allow you to run application code from the webpage, and with the webpage being remote, it would run under the 'web context' from a security perspective, so you'd not be able to use a lot of the native functionality you could run locally.
If the reason for re-developing the app in JS/HTML is to load code dynamically, I would advise against it. There are not a huge range of differences between a C# implementation and a JS implementation, so it would seem inefficient to redo all the existing work in a different language.

How to put my app to run "Always On Top" using Phonegap?

I need to run my app always on top of the android system, while the user will be able to exit the app only with his own password. I was told that in order to do that i need to setup and run a Service of my own.
My question is can and how it is done using Phonegap and Jquery. How do i define and run my own service? Thanks for your help.
First, don't do that. I know that doesn't sound like an answer, but trust me, it actually is the answer. What I mean is, no, you don't want your app to be "always on top of the android system." Use the actual Android lifecycle and participate as a good citizen, don't abuse it and try to keep your app always running. I know you may have a use case that makes you think you need to do that, but you most likely don't.
Second, you can write a native service (not JS) and keep it running in the background. This is a a standard Android service. You'll need to create a PhoneGap plugin to hook into your app via the service (or you can create this yourself outside PhoneGap, either way). Then you can either do work in the background, or bring your app to the foreground via the service.
This answer may help: Creating an Android Service with Phonegap? (Have phonegap app run even when closed)
Third, again, don't do that. Most long running services are a bad idea (not all mind you, some use cases require it, a music player, etc, but most do not). It's a much better idea to create an IntentService and schedule an alarm (via AlarmManager) to launch it in the background periodically if you just need to do some occasional background work (fetch data, etc). Regardless of the use case though, a long running background service will consume a lot of resources and make users justifiably wary of your app.
I don't think Phonegap will let you create a service solely in javascript. To be able to make a service, you will have to write at least some native code. See https://stackoverflow.com/a/10427975/1721527.
I've never used Phonegap, but I think you can create a native plugin to talk to the javascript. See http://docs.phonegap.com/en/2.1.0/guide_plugin-development_index.md.html.
See Android documentation on services at http://developer.android.com/guide/components/services.html.

Categories

Resources