getWorkerInterface
January 19, 2026 ยท View on GitHub
Description
When running the RxPlayer in a "multithreading" mode, it will rely on a WebWorker to run the core logic of the player.
In that situation, if you have advanced needs (importing certain features, implementing custom loading logic etc.), you may want to define your own worker-side code.
getWorkerInterface then allows your application to communicate with that worker.
If no worker is initialized through the current RxPlayer instance, it will return null.
If however a worker is initialized (you called the attachWorker method on the same
RxPlayer instance), it should return an object with the following properties:
-
sendMessage: A function allowing to send messages to the associated Worker.It takes two arguments:
-
messageName (
string): Name for the associated event that has been listened to through anaddMessageListenercall on the worker-side. Can be any string. -
payload (
Object): Payload for that particular event.It has to be cloneable through the structured clone algorithm.
-
-
addMessageListener: A function allowing to catch event sent by the worker-side, which has a mirror version of this interface. It will be received by the worker if itIt takes two arguments:
-
messageName (
string): Name for the event from the worker-side to listen to. -
callback (
function): The callback that will be called every time a message with the given messageName was sent by the worker. This callback takes the payload the worker sent as argument. Like when doing thesendMessageon this interface, payloads sent by the worker also have been cloned through the structured clone algorithm first.
-
-
removeMessageListener: A function allowing to remove a message listener previously added through anaddMessageListenercall. Takes two arguments which have to be the same ones than the one provided toaddMessageListener.
Example
const workerInterface = player.getWorkerInterface();
if (workerInterface === null) {
console.info("No worker attached."); // Note that this may also happen when an
// error prevented a worker from being attached.
} else {
// Example of a message that can be sent
workerInterface.sendMessage("my-content-info", {
title: "myContent",
id: 468742542,
});
// Listen for example to an event you produce worker-side when new media data
// is requested.
workerInterface.addMessageListener("my-request-info", (info) => {
console.log(info.url);
});
}
Syntax
const workerInterface = player.getWorkerInterface();
- return value
Object|null