Hello! I'm working on chess app which makes a lot of simulations for analytics and AI play mode. This freezes the UI and main thread for 2-3 seconds because of all processes running.
I had BIG research on how to solve that.
Firstly I want to mention that async functions and Promises do not help because they're running in the main thread and it still freezes. And also libraries like hamsters.js cannot help.
Also I found some RN libs and plugins for workers: https://github.com/fabriciovergal/react-native-workers, https://github.com/devfd/react-native-workers, which are no longer maintained and don't work with newer versions. There is one more library: https://github.com/joltup/react-native-threads. It also fails:
Also there's requestAnimation API wich cannot help in my case, because the whole processing is provided by library called RSG Chess API so I run:
AI(something)
instead of:
for (something) {
something
}
so I cannot break the computing into smaller processes.
Another possible solution is to use "invisible" WebView in the app and there run the processes and comunicating via the window.postMessage API. Link to blog post: https://medium.com/@inkdrop/a-simple-way-to-run-js-in-background-thread-on-react-native-8fff345576da. But that is very tricky and hacky.
It looks like your issue may be incomplete. Are all the fields required by the Issue Template filled out?
If you believe your issue contains all the relevant information, let us know in order to have a maintainer remove the No Template label.
If you are still encountering the issue described here, please open a new issue and make sure to fill out the Issue Template when doing so.
@radi-cho Can you tell me how did you fix the problem.
@vikas5914 Yup... As I described before there are already a lot of posts and articles about this topic. However they're incomplete and little bit tricky.
Every step is listed below. Before to read them please join my slackroom http://rsg-slack.herokuapp.com and I'll help you achieve multithreading. Also this solution is implemented in the RSG-Chess-mobile repository. Take a look here:
// in your js code
const html = `
<html>
<body>
<script>
// The code which will be run on the web thread.
</script>
</body>
</html>`;
If you want to use any node/ES6 modules or npm packages you'll need to make a new file and then compile it with webpack and babel or use online tool.
In my case I needed the RSG Chess API module and copy-pasted its code from the official repo, like that:

WebView to your app.// example from the Medium post, but using your custom html and scripts
<WebView
ref={el => this.webView = el}
source={{html: html}}
onMessage={this.handleMessage}
/>
the
htmlconst must be a string. You can also create html file and use the nodejs apis to pass it to the WebView.
<script>
window.AI = function (params) {
// code here
// return some response to the main RN JS thread
window.postMessage();
}
</script>
injectJavaScript method. In my case I used:this.webView.injectJavaScript(
`AI(${params})`
)
this.webViewis defined byref={el => this.webView = el}
In the example above
paramsis stringified object, because you must send and recieve only STRINGS via the postMessageAPI and theinjectJavaScriptmethod. Don't worry you can pass objects by usingJSON.stringifyandJSON.parse
window.postMessage(/* string, stringified object/array or stringified function */);
To recieve the data you can use handleMessage method predefined in the onMessage prop of the WebView.
<WebView onMessage={this.handleMessage} />
handleMessage = msg => {
msg.nativeEvent.data
}
Hopefully this will help!
thanks to @radi-cho and his project RSG Chess project, I solved the problem.
The example is:
https://pastebin.com/DG490YJw
https://pastebin.com/t6w1i9EX
I have react native 0.53.0.
So we with @invyctus92 resolved his issue. There were two problems with his code.
You need to pass source={{ html: html }} to the webView
Wrong case: source={{ html }} which will pass { html: true } to the WebView
Right case: source={{ html: html }} which will pass { html: CONTENT_IN_YOUR_html_VAR }
He needed to run the web thread in the componentDidMount method, but sometimes the webView is not yet loaded at this time. For that purpose you can use the state or a callback to check if everything is loaded.
Working examples:
Also I tried to make this issue as clean as possible, so If someone have additional questions please contact me here http://rsg-slack.herokuapp.com But if someone have better solution or code which will help the community please post here!
Most helpful comment
@vikas5914 Yup... As I described before there are already a lot of posts and articles about this topic. However they're incomplete and little bit tricky.
Example:
If you want to use any node/ES6 modules or npm packages you'll need to make a new file and then compile it with

webpackandbabelor use online tool.In my case I needed the
RSG Chess APImodule and copy-pasted its code from the official repo, like that:WebViewto your app.To run the methods from the main RN JS thread you can simple use the
injectJavaScriptmethod. In my case I used:When your tasks in the web thread are ready you can send back response to the main thread
To recieve the data you can use
handleMessagemethod predefined in theonMessageprop of the WebView.Hopefully this will help!