Skip to main content
torch.js has not been released yet.
torch.js logotorch.js logotorch.js
PlaygroundContact
Login
Documentation
IntroductionType SafetyTensor ExpressionsTensor IndexingEinsumEinopsAutogradTraining a ModelProfiling & MemoryPyTorch MigrationBest PracticesRuntimesPerformancePyTorch CompatibilityBenchmarksDType Coverage
torch.js· 2026
LegalTerms of UsePrivacy Policy
/
/
  1. docs
  2. Spark
  3. spark
  4. createWorkerRpc

spark.createWorkerRpc

function createWorkerRpc(worker: Worker): Rpc

Create an RPC instance for communicating with a Web Worker.

Convenience function that creates an RPC with appropriate defaults for worker communication (always uses origin '*' since workers don't support origin validation).

Equivalent to: new Rpc({ target: worker, origin: '*' })

Parameters

workerWorker
Web Worker instance to communicate with

Returns

Rpc– RPC instance ready to use with the worker

Examples

// Create worker and RPC
const worker = new Worker('my-worker.js');
const rpc = createWorkerRpc(worker);

// Register handlers for worker messages
rpc.handle('worker.status', (status) => {
  console.log('Worker status:', status);
});

// Call worker methods
const result = await rpc.call('train', { epochs: 100 });

// Clean up
rpc.destroy();
worker.terminate();

See Also

  • createParentRpc - Create RPC to parent window
  • createSelfRpc - Create RPC in worker context
Previous
createSelfRpc
Next
dataset