[{"data":1,"prerenderedAt":885},["ShallowReactive",2],{"navigation":3,"-adapters-cloudflare":72,"-adapters-cloudflare-surround":881},[4,36],{"title":5,"path":6,"stem":7,"children":8,"icon":10},"Getting Started","/guide","1.guide/1.index",[9,11,16,21,26,31],{"title":5,"path":6,"stem":7,"icon":10},"ph:book-open-duotone",{"title":12,"path":13,"stem":14,"icon":15},"Hooks","/guide/hooks","1.guide/2.hooks","material-symbols-light:data-object",{"title":17,"path":18,"stem":19,"icon":20},"Peer","/guide/peer","1.guide/3.peer","mynaui:api",{"title":22,"path":23,"stem":24,"icon":25},"Message","/guide/message","1.guide/4.message","solar:letter-line-duotone",{"title":27,"path":28,"stem":29,"icon":30},"Pub / Sub","/guide/pubsub","1.guide/5.pubsub","simple-icons:googlepubsub",{"title":32,"path":33,"stem":34,"icon":35},"Resolver API","/guide/resolver","1.guide/6.resolver","tabler:route",{"title":37,"path":38,"stem":39,"children":40,"icon":42},"Adapters","/adapters","2.adapters/1.index",[41,43,48,52,57,62,67],{"title":37,"path":38,"stem":39,"icon":42},"emojione-monotone:electric-plug",{"title":44,"path":45,"stem":46,"icon":47},"Bun","/adapters/bun","2.adapters/bun","simple-icons:bun",{"title":49,"path":50,"stem":51},"Bunny","/adapters/bunny","2.adapters/bunny",{"title":53,"path":54,"stem":55,"icon":56},"Cloudflare","/adapters/cloudflare","2.adapters/cloudflare","devicon-plain:cloudflareworkers",{"title":58,"path":59,"stem":60,"icon":61},"Deno","/adapters/deno","2.adapters/deno","teenyicons:deno-solid",{"title":63,"path":64,"stem":65,"icon":66},"Node.js","/adapters/node","2.adapters/node","akar-icons:node-fill",{"title":68,"path":69,"stem":70,"icon":71},"SSE","/adapters/sse","2.adapters/sse","clarity:two-way-arrows-line",{"id":73,"title":53,"body":74,"description":875,"extension":876,"meta":877,"navigation":878,"path":54,"seo":879,"stem":55,"__hash__":880},"content/2.adapters/cloudflare.md",{"type":75,"value":76,"toc":872,"icon":56},"minimark",[77,104,115,714,721,790,812,817,837,868],[78,79,80,81,88,89,92,93,98,99,103],"p",{},"To integrate crossws with Cloudflare ",[82,83,87],"a",{"href":84,"rel":85},"https://developers.cloudflare.com/durable-objects/api/websockets/",[86],"nofollow","Durable Objects"," with ",[82,90,91],{"href":28},"pub/sub"," and ",[82,94,97],{"href":95,"rel":96},"https://developers.cloudflare.com/durable-objects/best-practices/websockets/#websocket-hibernation-api",[86],"hibernation API"," support, you need to check for the ",[100,101,102],"code",{},"upgrade"," header and additionally export a DurableObject with crossws adapter hooks integrated.",[105,106,107],"note",{},[78,108,109,110,114],{},"\nIf you skip durable object class export or in cases the binding is unavailable, crossws uses a ",[111,112,113],"strong",{},"fallback mode"," without pub/sub support in the same worker.",[116,117,122],"pre",{"className":118,"code":119,"language":120,"meta":121,"style":121},"language-js shiki shiki-themes github-light github-dark github-dark","import { DurableObject } from \"cloudflare:workers\";\nimport crossws from \"crossws/adapters/cloudflare\";\n\nconst ws = crossws({\n  // bindingName: \"$DurableObject\",\n  // instanceName: \"crossws\",\n  hooks: {\n    message: console.log,\n    open(peer) {\n      peer.subscribe(\"chat\");\n      peer.publish(\"chat\", { user: \"server\", message: `${peer} joined!` });\n    },\n  },\n});\n\nexport default {\n  async fetch(request, env, context) {\n    if (request.headers.get(\"upgrade\") === \"websocket\") {\n      return ws.handleUpgrade(request, env, context);\n    }\n    return new Response(\n      `\u003Cscript>new WebSocket(\"ws://localhost:3000\").addEventListener(\"open\", (e) => e.target.send(\"Hello from client!\"));\u003C/script>`,\n      { headers: { \"content-type\": \"text/html\" } },\n    );\n  },\n};\n\nexport class $DurableObject extends DurableObject {\n  constructor(state, env) {\n    super(state, env);\n    ws.handleDurableInit(this, state, env);\n  }\n\n  fetch(request) {\n    return ws.handleDurableUpgrade(this, request);\n  }\n\n  webSocketMessage(client, message) {\n    return ws.handleDurableMessage(this, client, message);\n  }\n\n  webSocketPublish(topic, message, opts) {\n    return ws.handleDurablePublish(this, topic, message, opts);\n  }\n\n  webSocketClose(client, code, reason, wasClean) {\n    return ws.handleDurableClose(this, client, code, reason, wasClean);\n  }\n}\n","js","",[100,123,124,147,162,169,189,196,202,208,214,230,247,279,285,291,297,302,314,341,369,384,390,405,414,432,438,443,449,454,473,490,499,516,522,527,539,556,561,566,584,601,606,611,633,650,655,660,686,703,708],{"__ignoreMap":121},[125,126,129,133,137,140,144],"span",{"class":127,"line":128},"line",1,[125,130,132],{"class":131},"so5gQ","import",[125,134,136],{"class":135},"slsVL"," { DurableObject } ",[125,138,139],{"class":131},"from",[125,141,143],{"class":142},"sfrk1"," \"cloudflare:workers\"",[125,145,146],{"class":135},";\n",[125,148,150,152,155,157,160],{"class":127,"line":149},2,[125,151,132],{"class":131},[125,153,154],{"class":135}," crossws ",[125,156,139],{"class":131},[125,158,159],{"class":142}," \"crossws/adapters/cloudflare\"",[125,161,146],{"class":135},[125,163,165],{"class":127,"line":164},3,[125,166,168],{"emptyLinePlaceholder":167},true,"\n",[125,170,172,175,179,182,186],{"class":127,"line":171},4,[125,173,174],{"class":131},"const",[125,176,178],{"class":177},"suiK_"," ws",[125,180,181],{"class":131}," =",[125,183,185],{"class":184},"shcOC"," crossws",[125,187,188],{"class":135},"({\n",[125,190,192],{"class":127,"line":191},5,[125,193,195],{"class":194},"sCsY4","  // bindingName: \"$DurableObject\",\n",[125,197,199],{"class":127,"line":198},6,[125,200,201],{"class":194},"  // instanceName: \"crossws\",\n",[125,203,205],{"class":127,"line":204},7,[125,206,207],{"class":135},"  hooks: {\n",[125,209,211],{"class":127,"line":210},8,[125,212,213],{"class":135},"    message: console.log,\n",[125,215,217,220,223,227],{"class":127,"line":216},9,[125,218,219],{"class":184},"    open",[125,221,222],{"class":135},"(",[125,224,226],{"class":225},"sQHwn","peer",[125,228,229],{"class":135},") {\n",[125,231,233,236,239,241,244],{"class":127,"line":232},10,[125,234,235],{"class":135},"      peer.",[125,237,238],{"class":184},"subscribe",[125,240,222],{"class":135},[125,242,243],{"class":142},"\"chat\"",[125,245,246],{"class":135},");\n",[125,248,250,252,255,257,259,262,265,268,271,273,276],{"class":127,"line":249},11,[125,251,235],{"class":135},[125,253,254],{"class":184},"publish",[125,256,222],{"class":135},[125,258,243],{"class":142},[125,260,261],{"class":135},", { user: ",[125,263,264],{"class":142},"\"server\"",[125,266,267],{"class":135},", message: ",[125,269,270],{"class":142},"`${",[125,272,226],{"class":135},[125,274,275],{"class":142},"} joined!`",[125,277,278],{"class":135}," });\n",[125,280,282],{"class":127,"line":281},12,[125,283,284],{"class":135},"    },\n",[125,286,288],{"class":127,"line":287},13,[125,289,290],{"class":135},"  },\n",[125,292,294],{"class":127,"line":293},14,[125,295,296],{"class":135},"});\n",[125,298,300],{"class":127,"line":299},15,[125,301,168],{"emptyLinePlaceholder":167},[125,303,305,308,311],{"class":127,"line":304},16,[125,306,307],{"class":131},"export",[125,309,310],{"class":131}," default",[125,312,313],{"class":135}," {\n",[125,315,317,320,323,325,328,331,334,336,339],{"class":127,"line":316},17,[125,318,319],{"class":131},"  async",[125,321,322],{"class":184}," fetch",[125,324,222],{"class":135},[125,326,327],{"class":225},"request",[125,329,330],{"class":135},", ",[125,332,333],{"class":225},"env",[125,335,330],{"class":135},[125,337,338],{"class":225},"context",[125,340,229],{"class":135},[125,342,344,347,350,353,355,358,361,364,367],{"class":127,"line":343},18,[125,345,346],{"class":131},"    if",[125,348,349],{"class":135}," (request.headers.",[125,351,352],{"class":184},"get",[125,354,222],{"class":135},[125,356,357],{"class":142},"\"upgrade\"",[125,359,360],{"class":135},") ",[125,362,363],{"class":131},"===",[125,365,366],{"class":142}," \"websocket\"",[125,368,229],{"class":135},[125,370,372,375,378,381],{"class":127,"line":371},19,[125,373,374],{"class":131},"      return",[125,376,377],{"class":135}," ws.",[125,379,380],{"class":184},"handleUpgrade",[125,382,383],{"class":135},"(request, env, context);\n",[125,385,387],{"class":127,"line":386},20,[125,388,389],{"class":135},"    }\n",[125,391,393,396,399,402],{"class":127,"line":392},21,[125,394,395],{"class":131},"    return",[125,397,398],{"class":131}," new",[125,400,401],{"class":184}," Response",[125,403,404],{"class":135},"(\n",[125,406,408,411],{"class":127,"line":407},22,[125,409,410],{"class":142},"      `\u003Cscript>new WebSocket(\"ws://localhost:3000\").addEventListener(\"open\", (e) => e.target.send(\"Hello from client!\"));\u003C/script>`",[125,412,413],{"class":135},",\n",[125,415,417,420,423,426,429],{"class":127,"line":416},23,[125,418,419],{"class":135},"      { headers: { ",[125,421,422],{"class":142},"\"content-type\"",[125,424,425],{"class":135},": ",[125,427,428],{"class":142},"\"text/html\"",[125,430,431],{"class":135}," } },\n",[125,433,435],{"class":127,"line":434},24,[125,436,437],{"class":135},"    );\n",[125,439,441],{"class":127,"line":440},25,[125,442,290],{"class":135},[125,444,446],{"class":127,"line":445},26,[125,447,448],{"class":135},"};\n",[125,450,452],{"class":127,"line":451},27,[125,453,168],{"emptyLinePlaceholder":167},[125,455,457,459,462,465,468,471],{"class":127,"line":456},28,[125,458,307],{"class":131},[125,460,461],{"class":131}," class",[125,463,464],{"class":184}," $DurableObject",[125,466,467],{"class":131}," extends",[125,469,470],{"class":184}," DurableObject",[125,472,313],{"class":135},[125,474,476,479,481,484,486,488],{"class":127,"line":475},29,[125,477,478],{"class":131},"  constructor",[125,480,222],{"class":135},[125,482,483],{"class":225},"state",[125,485,330],{"class":135},[125,487,333],{"class":225},[125,489,229],{"class":135},[125,491,493,496],{"class":127,"line":492},30,[125,494,495],{"class":177},"    super",[125,497,498],{"class":135},"(state, env);\n",[125,500,502,505,508,510,513],{"class":127,"line":501},31,[125,503,504],{"class":135},"    ws.",[125,506,507],{"class":184},"handleDurableInit",[125,509,222],{"class":135},[125,511,512],{"class":177},"this",[125,514,515],{"class":135},", state, env);\n",[125,517,519],{"class":127,"line":518},32,[125,520,521],{"class":135},"  }\n",[125,523,525],{"class":127,"line":524},33,[125,526,168],{"emptyLinePlaceholder":167},[125,528,530,533,535,537],{"class":127,"line":529},34,[125,531,532],{"class":184},"  fetch",[125,534,222],{"class":135},[125,536,327],{"class":225},[125,538,229],{"class":135},[125,540,542,544,546,549,551,553],{"class":127,"line":541},35,[125,543,395],{"class":131},[125,545,377],{"class":135},[125,547,548],{"class":184},"handleDurableUpgrade",[125,550,222],{"class":135},[125,552,512],{"class":177},[125,554,555],{"class":135},", request);\n",[125,557,559],{"class":127,"line":558},36,[125,560,521],{"class":135},[125,562,564],{"class":127,"line":563},37,[125,565,168],{"emptyLinePlaceholder":167},[125,567,569,572,574,577,579,582],{"class":127,"line":568},38,[125,570,571],{"class":184},"  webSocketMessage",[125,573,222],{"class":135},[125,575,576],{"class":225},"client",[125,578,330],{"class":135},[125,580,581],{"class":225},"message",[125,583,229],{"class":135},[125,585,587,589,591,594,596,598],{"class":127,"line":586},39,[125,588,395],{"class":131},[125,590,377],{"class":135},[125,592,593],{"class":184},"handleDurableMessage",[125,595,222],{"class":135},[125,597,512],{"class":177},[125,599,600],{"class":135},", client, message);\n",[125,602,604],{"class":127,"line":603},40,[125,605,521],{"class":135},[125,607,609],{"class":127,"line":608},41,[125,610,168],{"emptyLinePlaceholder":167},[125,612,614,617,619,622,624,626,628,631],{"class":127,"line":613},42,[125,615,616],{"class":184},"  webSocketPublish",[125,618,222],{"class":135},[125,620,621],{"class":225},"topic",[125,623,330],{"class":135},[125,625,581],{"class":225},[125,627,330],{"class":135},[125,629,630],{"class":225},"opts",[125,632,229],{"class":135},[125,634,636,638,640,643,645,647],{"class":127,"line":635},43,[125,637,395],{"class":131},[125,639,377],{"class":135},[125,641,642],{"class":184},"handleDurablePublish",[125,644,222],{"class":135},[125,646,512],{"class":177},[125,648,649],{"class":135},", topic, message, opts);\n",[125,651,653],{"class":127,"line":652},44,[125,654,521],{"class":135},[125,656,658],{"class":127,"line":657},45,[125,659,168],{"emptyLinePlaceholder":167},[125,661,663,666,668,670,672,674,676,679,681,684],{"class":127,"line":662},46,[125,664,665],{"class":184},"  webSocketClose",[125,667,222],{"class":135},[125,669,576],{"class":225},[125,671,330],{"class":135},[125,673,100],{"class":225},[125,675,330],{"class":135},[125,677,678],{"class":225},"reason",[125,680,330],{"class":135},[125,682,683],{"class":225},"wasClean",[125,685,229],{"class":135},[125,687,689,691,693,696,698,700],{"class":127,"line":688},47,[125,690,395],{"class":131},[125,692,377],{"class":135},[125,694,695],{"class":184},"handleDurableClose",[125,697,222],{"class":135},[125,699,512],{"class":177},[125,701,702],{"class":135},", client, code, reason, wasClean);\n",[125,704,706],{"class":127,"line":705},48,[125,707,521],{"class":135},[125,709,711],{"class":127,"line":710},49,[125,712,713],{"class":135},"}\n",[78,715,716,717,720],{},"Update your ",[100,718,719],{},"wrangler.toml"," to specify Durable object:",[116,722,726],{"className":723,"code":724,"language":725,"meta":121,"style":121},"language-ini shiki shiki-themes github-light github-dark github-dark","[[durable_objects.bindings]]\nname = \"$DurableObject\"\nclass_name = \"$DurableObject\"\n\n[[migrations]]\ntag = \"v1\"\nnew_classes = [\"$DurableObject\"]\n","ini",[100,727,728,736,747,756,760,767,777],{"__ignoreMap":121},[125,729,730,733],{"class":127,"line":128},[125,731,732],{"class":184},"[[durable_objects.bindings]",[125,734,735],{"class":135},"]\n",[125,737,738,741,744],{"class":127,"line":149},[125,739,740],{"class":131},"name",[125,742,743],{"class":135}," = ",[125,745,746],{"class":142},"\"$DurableObject\"\n",[125,748,749,752,754],{"class":127,"line":164},[125,750,751],{"class":131},"class_name",[125,753,743],{"class":135},[125,755,746],{"class":142},[125,757,758],{"class":127,"line":171},[125,759,168],{"emptyLinePlaceholder":167},[125,761,762,765],{"class":127,"line":191},[125,763,764],{"class":184},"[[migrations]",[125,766,735],{"class":135},[125,768,769,772,774],{"class":127,"line":198},[125,770,771],{"class":131},"tag",[125,773,743],{"class":135},[125,775,776],{"class":142},"\"v1\"\n",[125,778,779,782,785,788],{"class":127,"line":204},[125,780,781],{"class":131},"new_classes",[125,783,784],{"class":135}," = [",[125,786,787],{"class":142},"\"$DurableObject\"",[125,789,735],{"class":135},[791,792,793],"read-more",{},[78,794,795,796,803,804,811],{},"See ",[82,797,800],{"href":798,"rel":799},"https://github.com/h3js/crossws/blob/main/test/fixture/cloudflare-durable.ts",[86],[100,801,802],{},"test/fixture/cloudflare-durable.ts"," for demo and ",[82,805,808],{"href":806,"rel":807},"https://github.com/h3js/crossws/blob/main/src/adapters/cloudflare.ts",[86],[100,809,810],{},"src/adapters/cloudflare.ts"," for implementation.",[813,814,816],"h3",{"id":815},"adapter-options","Adapter options",[105,818,819],{},[78,820,821,822,825,826,828,829,832,833,836],{},"\nBy default, crossws uses the durable object class ",[100,823,824],{},"$DurableObject"," from ",[100,827,333],{}," with an instance named ",[100,830,831],{},"crossws",".\nYou can customize this behavior by providing ",[100,834,835],{},"resolveDurableStub"," option.",[838,839,840,850,858],"ul",{},[841,842,843,846,847,849],"li",{},[100,844,845],{},"bindingName",": Durable Object binding name from environment (default: ",[100,848,824],{},").",[841,851,852,855,856,849],{},[100,853,854],{},"instanceName",": Durable Object instance name (default: ",[100,857,831],{},[841,859,860,862,863,92,865,867],{},[100,861,835],{},": Custom function that resolves Durable Object binding to handle the WebSocket upgrade. This option will override ",[100,864,845],{},[100,866,854],{},".",[869,870,871],"style",{},"html pre.shiki code .so5gQ, html code.shiki .so5gQ{--shiki-light:#D73A49;--shiki-default:#F97583;--shiki-dark:#F97583}html pre.shiki code .slsVL, html code.shiki .slsVL{--shiki-light:#24292E;--shiki-default:#E1E4E8;--shiki-dark:#E1E4E8}html pre.shiki code .sfrk1, html code.shiki .sfrk1{--shiki-light:#032F62;--shiki-default:#9ECBFF;--shiki-dark:#9ECBFF}html pre.shiki code .suiK_, html code.shiki .suiK_{--shiki-light:#005CC5;--shiki-default:#79B8FF;--shiki-dark:#79B8FF}html pre.shiki code .shcOC, html code.shiki .shcOC{--shiki-light:#6F42C1;--shiki-default:#B392F0;--shiki-dark:#B392F0}html pre.shiki code .sCsY4, html code.shiki .sCsY4{--shiki-light:#6A737D;--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .sQHwn, html code.shiki .sQHwn{--shiki-light:#E36209;--shiki-default:#FFAB70;--shiki-dark:#FFAB70}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":121,"searchDepth":149,"depth":149,"links":873},[874],{"id":815,"depth":164,"text":816},"Integrate crossws with Cloudflare Workers and Durable Objects.","md",{"icon":56},{"icon":56},{"title":53,"description":875},"yqwGc-LyarzTUzZBGY14WB2d4nQ7g4SNdr38PdKzHPo",[882,883],{"title":49,"path":50,"stem":51,"description":121,"children":-1},{"title":58,"path":59,"stem":60,"description":884,"icon":61,"children":-1},"Manually integrate crossws with Deno.",1771506027189]