Your machine supports multiple threads, each running as a real server-side thread. The number of threads available depends on your plan, but you can utilize several threads within a single machine.
Each thread has its own context, and you can communicate between threads using promises.
To start a thread, use the startThread function and provide the path to the file containing your thread code. This file must export at least one function, which will be called when the thread starts. You can export additional functions and invoke them from other threads as needed.
To dispose of a thread, call the dispose function on the ThreadRef object. This will immediately stop the thread.
Here is a simple example of setting up a thread and calling a function within it:
Main file (index.js):
let threadRef: ThreadRef bootstrap(async () => { threadRef = await startThread('./thread.js') const result = await threadRef.threadFunction('Hello world') console.log(result) })
Thread file (thread.js):
async function threadFunction(data) { return `Thread says: ${data}` } exposeCallables({ threadFunction, })
You can call functions from host in thread, but you need to use host object for that.
This object is available only in thread, and it has exposeCallables function, which you can use to expose functions to host.
All functions must be promisified, and you can not use any global objects in them.
Main file (index.js):
exposeCallables({ hostFunction: async (data) => { console.log('Host says:', data) }, })
Thread file (thread.js):
console.log(await host.hostFunction('Hello from thread'))
© 2025 Routzie Routzie.com