[{"data":1,"prerenderedAt":674},["ShallowReactive",2],{"navigation":3,"-adapters-node":72,"-adapters-node-surround":669},[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":63,"body":74,"description":663,"extension":664,"meta":665,"navigation":666,"path":64,"seo":667,"stem":65,"__hash__":668},"content/2.adapters/node.md",{"type":75,"value":76,"toc":660,"icon":66},"minimark",[77,98,117,380,402,407,416,423,638,656],[78,79,80],"tip",{},[81,82,83,84,88,89,92,93,97],"p",{},"\nYou can use ",[85,86,87],"code",{},"serve"," function from ",[85,90,91],{},"crossws/server"," to ",[94,95,96],"strong",{},"automatically"," integrate crossws with Node.js!",[81,99,100,101,104,105,108,109,116],{},"To manually integrate crossws with your Node.js HTTP server, you need to connect the ",[85,102,103],{},"upgrade"," event to the ",[85,106,107],{},"handleUpgrade"," method returned from the adapter. crossws uses a prebundled version of ",[110,111,115],"a",{"href":112,"rel":113},"https://github.com/websockets/ws",[114],"nofollow","ws",".",[118,119,124],"pre",{"className":120,"code":121,"language":122,"meta":123,"style":123},"language-ts shiki shiki-themes github-light github-dark github-dark","import { createServer } from \"node:http\";\nimport crossws from \"crossws/adapters/node\";\n\nconst ws = crossws({\n  hooks: {\n    message: console.log,\n  },\n});\n\nconst server = createServer((req, res) => {\n  res.end(\n    `\u003Cscript>new WebSocket(\"ws://localhost:3000\").addEventListener('open', (e) => e.target.send(\"Hello from client!\"));\u003C/script>`,\n  );\n}).listen(3000);\n\nserver.on(\"upgrade\", (req, socket, head) => {\n  if (req.headers.upgrade === \"websocket\") {\n    ws.handleUpgrade(req, socket, head);\n  }\n});\n","ts","",[85,125,126,149,164,171,191,197,203,209,215,220,255,267,276,282,300,305,340,358,369,375],{"__ignoreMap":123},[127,128,131,135,139,142,146],"span",{"class":129,"line":130},"line",1,[127,132,134],{"class":133},"so5gQ","import",[127,136,138],{"class":137},"slsVL"," { createServer } ",[127,140,141],{"class":133},"from",[127,143,145],{"class":144},"sfrk1"," \"node:http\"",[127,147,148],{"class":137},";\n",[127,150,152,154,157,159,162],{"class":129,"line":151},2,[127,153,134],{"class":133},[127,155,156],{"class":137}," crossws ",[127,158,141],{"class":133},[127,160,161],{"class":144}," \"crossws/adapters/node\"",[127,163,148],{"class":137},[127,165,167],{"class":129,"line":166},3,[127,168,170],{"emptyLinePlaceholder":169},true,"\n",[127,172,174,177,181,184,188],{"class":129,"line":173},4,[127,175,176],{"class":133},"const",[127,178,180],{"class":179},"suiK_"," ws",[127,182,183],{"class":133}," =",[127,185,187],{"class":186},"shcOC"," crossws",[127,189,190],{"class":137},"({\n",[127,192,194],{"class":129,"line":193},5,[127,195,196],{"class":137},"  hooks: {\n",[127,198,200],{"class":129,"line":199},6,[127,201,202],{"class":137},"    message: console.log,\n",[127,204,206],{"class":129,"line":205},7,[127,207,208],{"class":137},"  },\n",[127,210,212],{"class":129,"line":211},8,[127,213,214],{"class":137},"});\n",[127,216,218],{"class":129,"line":217},9,[127,219,170],{"emptyLinePlaceholder":169},[127,221,223,225,228,230,233,236,240,243,246,249,252],{"class":129,"line":222},10,[127,224,176],{"class":133},[127,226,227],{"class":179}," server",[127,229,183],{"class":133},[127,231,232],{"class":186}," createServer",[127,234,235],{"class":137},"((",[127,237,239],{"class":238},"sQHwn","req",[127,241,242],{"class":137},", ",[127,244,245],{"class":238},"res",[127,247,248],{"class":137},") ",[127,250,251],{"class":133},"=>",[127,253,254],{"class":137}," {\n",[127,256,258,261,264],{"class":129,"line":257},11,[127,259,260],{"class":137},"  res.",[127,262,263],{"class":186},"end",[127,265,266],{"class":137},"(\n",[127,268,270,273],{"class":129,"line":269},12,[127,271,272],{"class":144},"    `\u003Cscript>new WebSocket(\"ws://localhost:3000\").addEventListener('open', (e) => e.target.send(\"Hello from client!\"));\u003C/script>`",[127,274,275],{"class":137},",\n",[127,277,279],{"class":129,"line":278},13,[127,280,281],{"class":137},"  );\n",[127,283,285,288,291,294,297],{"class":129,"line":284},14,[127,286,287],{"class":137},"}).",[127,289,290],{"class":186},"listen",[127,292,293],{"class":137},"(",[127,295,296],{"class":179},"3000",[127,298,299],{"class":137},");\n",[127,301,303],{"class":129,"line":302},15,[127,304,170],{"emptyLinePlaceholder":169},[127,306,308,311,314,316,319,322,324,326,329,331,334,336,338],{"class":129,"line":307},16,[127,309,310],{"class":137},"server.",[127,312,313],{"class":186},"on",[127,315,293],{"class":137},[127,317,318],{"class":144},"\"upgrade\"",[127,320,321],{"class":137},", (",[127,323,239],{"class":238},[127,325,242],{"class":137},[127,327,328],{"class":238},"socket",[127,330,242],{"class":137},[127,332,333],{"class":238},"head",[127,335,248],{"class":137},[127,337,251],{"class":133},[127,339,254],{"class":137},[127,341,343,346,349,352,355],{"class":129,"line":342},17,[127,344,345],{"class":133},"  if",[127,347,348],{"class":137}," (req.headers.upgrade ",[127,350,351],{"class":133},"===",[127,353,354],{"class":144}," \"websocket\"",[127,356,357],{"class":137},") {\n",[127,359,361,364,366],{"class":129,"line":360},18,[127,362,363],{"class":137},"    ws.",[127,365,107],{"class":186},[127,367,368],{"class":137},"(req, socket, head);\n",[127,370,372],{"class":129,"line":371},19,[127,373,374],{"class":137},"  }\n",[127,376,378],{"class":129,"line":377},20,[127,379,214],{"class":137},[381,382,383],"read-more",{},[81,384,385,386,393,394,401],{},"See ",[110,387,390],{"href":388,"rel":389},"https://github.com/h3js/crossws/blob/main/test/fixture/node.ts",[114],[85,391,392],{},"test/fixture/node.ts"," for demo and ",[110,395,398],{"href":396,"rel":397},"https://github.com/h3js/crossws/blob/main/src/adapters/node.ts",[114],[85,399,400],{},"src/adapters/node.ts"," for implementation.",[403,404,406],"h2",{"id":405},"uwebsockets","uWebSockets",[81,408,409,410,415],{},"You can alternatively use ",[110,411,414],{"href":412,"rel":413},"https://github.com/uNetworking/uWebSockets.js",[114],"uWebSockets.js"," for Node.js servers.",[81,417,418,419,422],{},"First add ",[85,420,421],{},"uNetworking/uWebSockets.js"," as a dependency.",[118,424,426],{"className":120,"code":425,"language":122,"meta":123,"style":123},"import { App } from \"uWebSockets.js\";\nimport crossws from \"crossws/adapters/uws\";\n\nconst ws = crossws({\n  hooks: {\n    message: console.log,\n  },\n});\n\nconst server = App().ws(\"/*\", ws.websocket);\n\nserver.get(\"/*\", (res, req) => {\n  res.writeStatus(\"200 OK\").writeHeader(\"Content-Type\", \"text/html\");\n  res.end(\n    `\u003Cscript>new WebSocket(\"ws://localhost:3000\").addEventListener('open', (e) => e.target.send(\"Hello from client!\"));\u003C/script>`,\n  );\n});\n\nserver.listen(3001, () => {\n  console.log(\"Listening to port 3001\");\n});\n",[85,427,428,442,455,459,471,475,479,483,487,491,515,519,544,574,582,588,592,596,600,618,633],{"__ignoreMap":123},[127,429,430,432,435,437,440],{"class":129,"line":130},[127,431,134],{"class":133},[127,433,434],{"class":137}," { App } ",[127,436,141],{"class":133},[127,438,439],{"class":144}," \"uWebSockets.js\"",[127,441,148],{"class":137},[127,443,444,446,448,450,453],{"class":129,"line":151},[127,445,134],{"class":133},[127,447,156],{"class":137},[127,449,141],{"class":133},[127,451,452],{"class":144}," \"crossws/adapters/uws\"",[127,454,148],{"class":137},[127,456,457],{"class":129,"line":166},[127,458,170],{"emptyLinePlaceholder":169},[127,460,461,463,465,467,469],{"class":129,"line":173},[127,462,176],{"class":133},[127,464,180],{"class":179},[127,466,183],{"class":133},[127,468,187],{"class":186},[127,470,190],{"class":137},[127,472,473],{"class":129,"line":193},[127,474,196],{"class":137},[127,476,477],{"class":129,"line":199},[127,478,202],{"class":137},[127,480,481],{"class":129,"line":205},[127,482,208],{"class":137},[127,484,485],{"class":129,"line":211},[127,486,214],{"class":137},[127,488,489],{"class":129,"line":217},[127,490,170],{"emptyLinePlaceholder":169},[127,492,493,495,497,499,502,505,507,509,512],{"class":129,"line":222},[127,494,176],{"class":133},[127,496,227],{"class":179},[127,498,183],{"class":133},[127,500,501],{"class":186}," App",[127,503,504],{"class":137},"().",[127,506,115],{"class":186},[127,508,293],{"class":137},[127,510,511],{"class":144},"\"/*\"",[127,513,514],{"class":137},", ws.websocket);\n",[127,516,517],{"class":129,"line":257},[127,518,170],{"emptyLinePlaceholder":169},[127,520,521,523,526,528,530,532,534,536,538,540,542],{"class":129,"line":269},[127,522,310],{"class":137},[127,524,525],{"class":186},"get",[127,527,293],{"class":137},[127,529,511],{"class":144},[127,531,321],{"class":137},[127,533,245],{"class":238},[127,535,242],{"class":137},[127,537,239],{"class":238},[127,539,248],{"class":137},[127,541,251],{"class":133},[127,543,254],{"class":137},[127,545,546,548,551,553,556,559,562,564,567,569,572],{"class":129,"line":278},[127,547,260],{"class":137},[127,549,550],{"class":186},"writeStatus",[127,552,293],{"class":137},[127,554,555],{"class":144},"\"200 OK\"",[127,557,558],{"class":137},").",[127,560,561],{"class":186},"writeHeader",[127,563,293],{"class":137},[127,565,566],{"class":144},"\"Content-Type\"",[127,568,242],{"class":137},[127,570,571],{"class":144},"\"text/html\"",[127,573,299],{"class":137},[127,575,576,578,580],{"class":129,"line":284},[127,577,260],{"class":137},[127,579,263],{"class":186},[127,581,266],{"class":137},[127,583,584,586],{"class":129,"line":302},[127,585,272],{"class":144},[127,587,275],{"class":137},[127,589,590],{"class":129,"line":307},[127,591,281],{"class":137},[127,593,594],{"class":129,"line":342},[127,595,214],{"class":137},[127,597,598],{"class":129,"line":360},[127,599,170],{"emptyLinePlaceholder":169},[127,601,602,604,606,608,611,614,616],{"class":129,"line":371},[127,603,310],{"class":137},[127,605,290],{"class":186},[127,607,293],{"class":137},[127,609,610],{"class":179},"3001",[127,612,613],{"class":137},", () ",[127,615,251],{"class":133},[127,617,254],{"class":137},[127,619,620,623,626,628,631],{"class":129,"line":377},[127,621,622],{"class":137},"  console.",[127,624,625],{"class":186},"log",[127,627,293],{"class":137},[127,629,630],{"class":144},"\"Listening to port 3001\"",[127,632,299],{"class":137},[127,634,636],{"class":129,"line":635},21,[127,637,214],{"class":137},[381,639,640],{},[81,641,385,642,393,649,401],{},[110,643,646],{"href":644,"rel":645},"https://github.com/h3js/crossws/blob/main/test/fixture/node-uws.ts",[114],[85,647,648],{},"test/fixture/node-uws.ts",[110,650,653],{"href":651,"rel":652},"https://github.com/h3js/crossws/blob/main/src/adapters/node-uws.ts",[114],[85,654,655],{},"src/adapters/node-uws.ts",[657,658,659],"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 .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":123,"searchDepth":151,"depth":151,"links":661},[662],{"id":405,"depth":151,"text":406},"Integrate crossws with Node.js (manually) or uWebSockets.js","md",{"icon":66},{"icon":66},{"title":63,"description":663},"IozwMBt8Tj4UM_MsVuoMdN-lBhOw4tCy_J-LOO2523s",[670,672],{"title":58,"path":59,"stem":60,"description":671,"icon":61,"children":-1},"Manually integrate crossws with Deno.",{"title":68,"path":69,"stem":70,"description":673,"icon":71,"children":-1},"Integrate crossws with server-sent events and fetch-api.",1771506027190]