DEV Community

Lam
Lam

Posted on

2 1

Web Workers Cheat Sheet

[Web workers] Message data

[MessageEvent]

bubbles: false
cancelBubble: false
cancelable: false
clipboardData: undefined
currentTarget: Worker
data: "Hello"              the data
defaultPrevented: false
eventPhase: 0
lastEventId: ""
origin: ""
ports: Array[0]
returnValue: true
source: null
srcElement: Worker
target: Worker
timeStamp: 1344821022383
type: "message"
Enter fullscreen mode Exit fullscreen mode

These are the contents of message on onmessage.

Web workers

Client

var worker = new Worker('worker.js')

worker.onmessage = function (message) {
  alert(JSON.stringify(message.data))
})

worker.postMessage('hello!')
Enter fullscreen mode Exit fullscreen mode

Messages can be anything that can be serialized into JSON (objects, arrays, strings, numbers, booleans). See: structured clone

Worker

self.onmessage = function (message) {
  ···
}

self.postMessage({ msg: 'hello' })
Enter fullscreen mode Exit fullscreen mode

Reference

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more