[{"data":1,"prerenderedAt":1483},["ShallowReactive",2],{"$f8LEBDHsYbIyUBIgwZn7vX79haRzbnyaR07tLGuSSW1o":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":1465,"editUrl":1477,"prev":1478,"next":1481},"techniques\u002Fwebsockets","WebSockets","Build realtime features with WebSocket gateways, rooms and a shared WsServer.","---\ntitle: WebSockets\ndescription: Build realtime features with WebSocket gateways, rooms and a shared WsServer.\n---\n\n# WebSockets\n\n`ferrite-ws` adds WebSocket support with a **gateway** model — the same shape as NestJS's `@WebSocketGateway`. Gateways are providers that declare a path, handle connections and messages, and get access to a server with rooms and broadcasting.\n\n## Enable it\n\n```cargo\nfr add ws\n```\n\n```rust\n#[module(imports = [WsModuleImpl])]\npub struct AppModule;\n\n\u002F\u002F in main.rs, mount onto the router:\nlet app = Ferrite::create::\u003CAppModule>().await;\nferrite_ws::mount_on(&mut app.router, app.container.clone(), \"\u002Fws\");\napp.listen(\"0.0.0.0:3000\").await?;\n```\n\nThe mount prefix plus each gateway's path forms the socket URL, e.g. `ws:\u002F\u002Flocalhost:3000\u002Fws\u002Fchat`.\n\n## A gateway\n\n```rust\nuse ferrite_ws::{Gateway, WsContext, WsError, OutgoingMessage, submit_gateway};\nuse ferrite_framework::{injectable, inject};\nuse serde_json::json;\nuse std::sync::Arc;\n\n#[injectable]\npub struct ChatGateway {\n    messages: MessagesService,\n}\n\nimpl ChatGateway {\n    #[inject]\n    pub fn new(messages: MessagesService) -> Self {\n        Self { messages }\n    }\n}\n\n#[ferrite_framework::async_trait]\nimpl Gateway for ChatGateway {\n    fn path() -> &'static str {\n        \"\u002Fws\u002Fchat\"\n    }\n\n    async fn handle_connection(&self, ctx: Arc\u003CWsContext>) -> Result\u003C(), WsError> {\n        let _ = ctx.emit(OutgoingMessage::event(\"chat:hello\", json!({\"msg\": \"welcome\"})));\n        Ok(())\n    }\n\n    async fn handle_message(\n        &self,\n        ctx: Arc\u003CWsContext>,\n        event: &str,\n        data: serde_json::Value,\n    ) -> Result\u003COption\u003COutgoingMessage>, WsError> {\n        match event {\n            \"chat:send\" => {\n                let id = ctx.id;\n                let msg = OutgoingMessage::event(\"chat:broadcast\", json!({\"from\": id, \"data\": data}));\n                ctx.broadcast(msg.clone()).await;\n                Ok(Some(msg))\n            }\n            _ => Ok(None),\n        }\n    }\n\n    async fn handle_disconnect(&self, ctx: Arc\u003CWsContext>) {\n        let _ = ctx.broadcast(OutgoingMessage::event(\"chat:left\", json!({\"id\": ctx.id})));\n    }\n}\n\nsubmit_gateway!(ChatGateway, \"\u002Fws\u002Fchat\");\n```\n\n### The `Gateway` trait\n\n| Method | Default | Called |\n| --- | --- | --- |\n| `path() -> &'static str` | `\"\u002Fws\"` | registration |\n| `handle_connection(&self, ctx: Arc\u003CWsContext>)` | no-op | on connect |\n| `handle_message(&self, ctx, event, data)` | no-op | per inbound message |\n| `handle_disconnect(&self, ctx)` | no-op | on disconnect |\n\n`submit_gateway!(Type, \"\u002Fpath\")` registers the gateway into the inventory so `mount_on` can find it.\n\n## Wire format\n\nMessages are JSON envelopes with an optional `event`:\n\n```json\n{ \"event\": \"chat:broadcast\", \"data\": { \"from\": \"...\", \"text\": \"hi\" } }\n```\n\n```rust\nIncomingMessage { event: String, data: Value }\nOutgoingMessage::event(\"chat:history\", json!(...))\nOutgoingMessage::from(value)          \u002F\u002F event-less\nOutgoingMessage::from(\"plain string\")\n```\n\n## `WsContext`\n\nPer-connection context:\n\n```rust\nctx.id            \u002F\u002F SocketId (Uuid)\nctx.path          \u002F\u002F connected path\nctx.query         \u002F\u002F query string\nctx.server()      \u002F\u002F handle to the WsServer\n\nctx.emit(msg).await;        \u002F\u002F send to this socket\nctx.broadcast(msg).await;   \u002F\u002F send to all sockets\n```\n\n## `WsServer` — rooms & broadcast\n\nThe global singleton server:\n\n| Method | Purpose |\n| --- | --- |\n| `count() -> usize` | Connected sockets |\n| `send_to(id, msg)` | Direct message |\n| `broadcast(msg)` | All sockets |\n| `emit_to_room(room, msg)` | One room |\n| `join_room(id, room)` \u002F `leave_room(id, room)` | Room membership |\n\n```rust\nserver.join_room(ctx.id, \"general\");\nserver.emit_to_room(\"general\", OutgoingMessage::event(\"room:update\", json!({\"n\": 2})));\n```\n\n## Errors\n\n`WsError` covers `Protocol(String)`, `Json(serde_json::Error)`, `Closed`, and `Gateway(String)`, with a `From\u003Caxum::Error>` conversion.\n\n## Example\n\nThe repo ships a full `mono-ws-chat` example (port 3003) with a chat gateway, history, rooms, and an e2e test.\n\n## Next steps\n\n- [gRPC](\u002Fdocs\u002Ftechniques\u002Fgrpc).\n- [Messaging](\u002Fdocs\u002Ftechniques\u002Fmessaging).",{"frontmatter":9,"meta":10,"nodes":44},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,19,26,29,32,35,38,41],{"id":17,"depth":13,"text":18},"enable-it","Enable it",{"id":20,"depth":13,"text":21,"children":22},"a-gateway","A gateway",[23],{"id":24,"depth":14,"text":25},"a-gateway-the-gateway-trait","The Gateway trait",{"id":27,"depth":13,"text":28},"wire-format","Wire format",{"id":30,"depth":13,"text":31},"wscontext","WsContext",{"id":33,"depth":13,"text":34},"wsserver-rooms-broadcast","WsServer — rooms & broadcast",{"id":36,"depth":13,"text":37},"errors","Errors",{"id":39,"depth":13,"text":40},"example","Example",{"id":42,"depth":13,"text":43},"next-steps","Next steps",[45,49,66,69,78,234,240,242,940,947,1021,1030,1032,1037,1083,1166,1170,1173,1268,1274,1277,1344,1412,1414,1437,1439,1446,1448],[46,47,5],"h1",{"id":48},"websockets",[50,51,52,56,57,61,62,65],"p",{},[53,54,55],"code",{},"ferrite-ws"," adds WebSocket support with a ",[58,59,60],"strong",{},"gateway"," model — the same shape as NestJS's ",[53,63,64],{},"@WebSocketGateway",". Gateways are providers that declare a path, handle connections and messages, and get access to a server with rooms and broadcasting.",[67,68,18],"h2",{"id":17},[70,71,74],"pre",{"language":72,"class":73},"cargo","shiki dark:github-dark",[53,75,77],{"class":76},"language-cargo","fr add ws",[70,79,82],{"language":80,"class":81},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[53,83,85,108,109,108,123,108,125,108,131,108,166,108,211],{"class":84},"language-rust",[86,87,90,94,98,101,105],"span",{"class":88,"style":89},"line","display: inline",[86,91,93],{"style":92},"color:#24292E;--shiki-dark:#E1E4E8","#[module(imports ",[86,95,97],{"style":96},"color:#D73A49;--shiki-dark:#F97583","=",[86,99,100],{"style":92}," [",[86,102,104],{"style":103},"color:#6F42C1;--shiki-dark:#B392F0","WsModuleImpl",[86,106,107],{"style":92},"])]","\n",[86,110,111,114,117,120],{"class":88,"style":89},[86,112,113],{"style":96},"pub",[86,115,116],{"style":96}," struct",[86,118,119],{"style":103}," AppModule",[86,121,122],{"style":92},";",[86,124],{"class":88,"style":89},[86,126,127],{"class":88,"style":89},[86,128,130],{"style":129},"color:#6A737D;--shiki-dark:#6A737D","\u002F\u002F in main.rs, mount onto the router:",[86,132,133,136,139,141,144,147,150,152,155,158,161,164],{"class":88,"style":89},[86,134,135],{"style":96},"let",[86,137,138],{"style":92}," app ",[86,140,97],{"style":96},[86,142,143],{"style":103}," Ferrite",[86,145,146],{"style":96},"::",[86,148,149],{"style":103},"create",[86,151,146],{"style":96},[86,153,154],{"style":92},"\u003C",[86,156,157],{"style":103},"AppModule",[86,159,160],{"style":92},">()",[86,162,163],{"style":96},".await",[86,165,122],{"style":92},[86,167,168,171,173,176,179,182,185,188,191,193,196,198,201,204,208],{"class":88,"style":89},[86,169,170],{"style":103},"ferrite_ws",[86,172,146],{"style":96},[86,174,175],{"style":103},"mount_on",[86,177,178],{"style":92},"(",[86,180,181],{"style":96},"&mut",[86,183,184],{"style":92}," app",[86,186,187],{"style":96},".",[86,189,190],{"style":92},"router, app",[86,192,187],{"style":96},[86,194,195],{"style":92},"container",[86,197,187],{"style":96},[86,199,200],{"style":103},"clone",[86,202,203],{"style":92},"(), ",[86,205,207],{"style":206},"color:#032F62;--shiki-dark:#9ECBFF","\"\u002Fws\"",[86,209,210],{"style":92},");",[86,212,213,216,218,221,223,226,229,232],{"class":88,"style":89},[86,214,215],{"style":92},"app",[86,217,187],{"style":96},[86,219,220],{"style":103},"listen",[86,222,178],{"style":92},[86,224,225],{"style":206},"\"0.0.0.0:3000\"",[86,227,228],{"style":92},")",[86,230,231],{"style":96},".await?",[86,233,122],{"style":92},[50,235,236,237,187],{},"The mount prefix plus each gateway's path forms the socket URL, e.g. ",[53,238,239],{},"ws:\u002F\u002Flocalhost:3000\u002Fws\u002Fchat",[67,241,21],{"id":20},[70,243,244],{"language":80,"class":81},[53,245,246,108,280,108,292,108,304,108,323,108,325,108,330,108,342,108,356,108,361,108,363,108,372,108,377,108,407,108,415,108,420,108,424,108,426,108,436,108,450,108,477,108,482,108,486,108,488,108,534,108,585,108,593,108,597,108,599,108,610,108,619,108,635,108,649,108,665,108,690,108,698,108,708,108,725,108,768,108,792,108,805,108,810,108,829,108,834,108,838,108,840,108,868,108,913,108,917,108,921,108,923],{"class":84},[86,247,248,251,254,256,259,262,265,267,269,272,274,277],{"class":88,"style":89},[86,249,250],{"style":96},"use",[86,252,253],{"style":103}," ferrite_ws",[86,255,146],{"style":96},[86,257,258],{"style":92},"{",[86,260,261],{"style":103},"Gateway",[86,263,264],{"style":92},", ",[86,266,31],{"style":103},[86,268,264],{"style":92},[86,270,271],{"style":103},"WsError",[86,273,264],{"style":92},[86,275,276],{"style":103},"OutgoingMessage",[86,278,279],{"style":92},", submit_gateway};",[86,281,282,284,287,289],{"class":88,"style":89},[86,283,250],{"style":96},[86,285,286],{"style":103}," ferrite_framework",[86,288,146],{"style":96},[86,290,291],{"style":92},"{injectable, inject};",[86,293,294,296,299,301],{"class":88,"style":89},[86,295,250],{"style":96},[86,297,298],{"style":103}," serde_json",[86,300,146],{"style":96},[86,302,303],{"style":92},"json;",[86,305,306,308,311,313,316,318,321],{"class":88,"style":89},[86,307,250],{"style":96},[86,309,310],{"style":103}," std",[86,312,146],{"style":96},[86,314,315],{"style":103},"sync",[86,317,146],{"style":96},[86,319,320],{"style":103},"Arc",[86,322,122],{"style":92},[86,324],{"class":88,"style":89},[86,326,327],{"class":88,"style":89},[86,328,329],{"style":92},"#[injectable]",[86,331,332,334,336,339],{"class":88,"style":89},[86,333,113],{"style":96},[86,335,116],{"style":96},[86,337,338],{"style":103}," ChatGateway",[86,340,341],{"style":92}," {",[86,343,344,347,350,353],{"class":88,"style":89},[86,345,346],{"style":92},"    messages",[86,348,349],{"style":96},":",[86,351,352],{"style":103}," MessagesService",[86,354,355],{"style":92},",",[86,357,358],{"class":88,"style":89},[86,359,360],{"style":92},"}",[86,362],{"class":88,"style":89},[86,364,365,368,370],{"class":88,"style":89},[86,366,367],{"style":96},"impl",[86,369,338],{"style":103},[86,371,341],{"style":92},[86,373,374],{"class":88,"style":89},[86,375,376],{"style":92},"    #[inject]",[86,378,379,382,385,388,391,393,395,398,401,405],{"class":88,"style":89},[86,380,381],{"style":96},"    pub",[86,383,384],{"style":96}," fn",[86,386,387],{"style":103}," new",[86,389,390],{"style":92},"(messages",[86,392,349],{"style":96},[86,394,352],{"style":103},[86,396,397],{"style":92},") ",[86,399,400],{"style":96},"->",[86,402,404],{"style":403},"color:#005CC5;--shiki-dark:#79B8FF"," Self",[86,406,341],{"style":92},[86,408,409,412],{"class":88,"style":89},[86,410,411],{"style":403},"        Self",[86,413,414],{"style":92}," { messages }",[86,416,417],{"class":88,"style":89},[86,418,419],{"style":92},"    }",[86,421,422],{"class":88,"style":89},[86,423,360],{"style":92},[86,425],{"class":88,"style":89},[86,427,428,431,433],{"class":88,"style":89},[86,429,430],{"style":92},"#[ferrite_framework",[86,432,146],{"style":96},[86,434,435],{"style":92},"async_trait]",[86,437,438,440,443,446,448],{"class":88,"style":89},[86,439,367],{"style":96},[86,441,442],{"style":103}," Gateway",[86,444,445],{"style":96}," for",[86,447,338],{"style":103},[86,449,341],{"style":92},[86,451,452,455,458,461,463,466,469,472,475],{"class":88,"style":89},[86,453,454],{"style":96},"    fn",[86,456,457],{"style":103}," path",[86,459,460],{"style":92},"() ",[86,462,400],{"style":96},[86,464,465],{"style":96}," &",[86,467,468],{"style":92},"'",[86,470,471],{"style":103},"static",[86,473,474],{"style":103}," str",[86,476,341],{"style":92},[86,478,479],{"class":88,"style":89},[86,480,481],{"style":206},"        \"\u002Fws\u002Fchat\"",[86,483,484],{"class":88,"style":89},[86,485,419],{"style":92},[86,487],{"class":88,"style":89},[86,489,490,493,495,498,500,503,506,509,511,514,516,518,521,523,526,529,531],{"class":88,"style":89},[86,491,492],{"style":96},"    async",[86,494,384],{"style":96},[86,496,497],{"style":103}," handle_connection",[86,499,178],{"style":92},[86,501,502],{"style":96},"&",[86,504,505],{"style":403},"self",[86,507,508],{"style":92},", ctx",[86,510,349],{"style":96},[86,512,513],{"style":103}," Arc",[86,515,154],{"style":92},[86,517,31],{"style":103},[86,519,520],{"style":92},">) ",[86,522,400],{"style":96},[86,524,525],{"style":103}," Result",[86,527,528],{"style":92},"\u003C(), ",[86,530,271],{"style":103},[86,532,533],{"style":92},"> {",[86,535,536,539,542,544,547,549,552,554,556,558,561,563,566,568,571,574,577,579,582],{"class":88,"style":89},[86,537,538],{"style":96},"        let",[86,540,541],{"style":92}," _ ",[86,543,97],{"style":96},[86,545,546],{"style":92}," ctx",[86,548,187],{"style":96},[86,550,551],{"style":103},"emit",[86,553,178],{"style":92},[86,555,276],{"style":103},[86,557,146],{"style":96},[86,559,560],{"style":103},"event",[86,562,178],{"style":92},[86,564,565],{"style":206},"\"chat:hello\"",[86,567,264],{"style":92},[86,569,570],{"style":103},"json!",[86,572,573],{"style":92},"({",[86,575,576],{"style":206},"\"msg\"",[86,578,349],{"style":96},[86,580,581],{"style":206}," \"welcome\"",[86,583,584],{"style":92},"})));",[86,586,587,590],{"class":88,"style":89},[86,588,589],{"style":103},"        Ok",[86,591,592],{"style":92},"(())",[86,594,595],{"class":88,"style":89},[86,596,419],{"style":92},[86,598],{"class":88,"style":89},[86,600,601,603,605,608],{"class":88,"style":89},[86,602,492],{"style":96},[86,604,384],{"style":96},[86,606,607],{"style":103}," handle_message",[86,609,178],{"style":92},[86,611,612,615,617],{"class":88,"style":89},[86,613,614],{"style":96},"        &",[86,616,505],{"style":403},[86,618,355],{"style":92},[86,620,621,624,626,628,630,632],{"class":88,"style":89},[86,622,623],{"style":92},"        ctx",[86,625,349],{"style":96},[86,627,513],{"style":103},[86,629,154],{"style":92},[86,631,31],{"style":103},[86,633,634],{"style":92},">,",[86,636,637,640,642,644,647],{"class":88,"style":89},[86,638,639],{"style":92},"        event",[86,641,349],{"style":96},[86,643,465],{"style":96},[86,645,646],{"style":103},"str",[86,648,355],{"style":92},[86,650,651,654,656,658,660,663],{"class":88,"style":89},[86,652,653],{"style":92},"        data",[86,655,349],{"style":96},[86,657,298],{"style":103},[86,659,146],{"style":96},[86,661,662],{"style":103},"Value",[86,664,355],{"style":92},[86,666,667,670,672,674,676,679,681,683,686,688],{"class":88,"style":89},[86,668,669],{"style":92},"    ) ",[86,671,400],{"style":96},[86,673,525],{"style":103},[86,675,154],{"style":92},[86,677,678],{"style":103},"Option",[86,680,154],{"style":92},[86,682,276],{"style":103},[86,684,685],{"style":92},">, ",[86,687,271],{"style":103},[86,689,533],{"style":92},[86,691,692,695],{"class":88,"style":89},[86,693,694],{"style":96},"        match",[86,696,697],{"style":92}," event {",[86,699,700,703,706],{"class":88,"style":89},[86,701,702],{"style":206},"            \"chat:send\"",[86,704,705],{"style":96}," =>",[86,707,341],{"style":92},[86,709,710,713,716,718,720,722],{"class":88,"style":89},[86,711,712],{"style":96},"                let",[86,714,715],{"style":92}," id ",[86,717,97],{"style":96},[86,719,546],{"style":92},[86,721,187],{"style":96},[86,723,724],{"style":92},"id;",[86,726,727,729,732,734,737,739,741,743,746,748,750,752,755,757,760,763,765],{"class":88,"style":89},[86,728,712],{"style":96},[86,730,731],{"style":92}," msg ",[86,733,97],{"style":96},[86,735,736],{"style":103}," OutgoingMessage",[86,738,146],{"style":96},[86,740,560],{"style":103},[86,742,178],{"style":92},[86,744,745],{"style":206},"\"chat:broadcast\"",[86,747,264],{"style":92},[86,749,570],{"style":103},[86,751,573],{"style":92},[86,753,754],{"style":206},"\"from\"",[86,756,349],{"style":96},[86,758,759],{"style":92}," id, ",[86,761,762],{"style":206},"\"data\"",[86,764,349],{"style":96},[86,766,767],{"style":92}," data}));",[86,769,770,773,775,778,781,783,785,788,790],{"class":88,"style":89},[86,771,772],{"style":92},"                ctx",[86,774,187],{"style":96},[86,776,777],{"style":103},"broadcast",[86,779,780],{"style":92},"(msg",[86,782,187],{"style":96},[86,784,200],{"style":103},[86,786,787],{"style":92},"())",[86,789,163],{"style":96},[86,791,122],{"style":92},[86,793,794,797,799,802],{"class":88,"style":89},[86,795,796],{"style":103},"                Ok",[86,798,178],{"style":92},[86,800,801],{"style":103},"Some",[86,803,804],{"style":92},"(msg))",[86,806,807],{"class":88,"style":89},[86,808,809],{"style":92},"            }",[86,811,812,815,818,821,823,826],{"class":88,"style":89},[86,813,814],{"style":92},"            _ ",[86,816,817],{"style":96},"=>",[86,819,820],{"style":103}," Ok",[86,822,178],{"style":92},[86,824,825],{"style":103},"None",[86,827,828],{"style":92},"),",[86,830,831],{"class":88,"style":89},[86,832,833],{"style":92},"        }",[86,835,836],{"class":88,"style":89},[86,837,419],{"style":92},[86,839],{"class":88,"style":89},[86,841,842,844,846,849,851,853,855,857,859,861,863,865],{"class":88,"style":89},[86,843,492],{"style":96},[86,845,384],{"style":96},[86,847,848],{"style":103}," handle_disconnect",[86,850,178],{"style":92},[86,852,502],{"style":96},[86,854,505],{"style":403},[86,856,508],{"style":92},[86,858,349],{"style":96},[86,860,513],{"style":103},[86,862,154],{"style":92},[86,864,31],{"style":103},[86,866,867],{"style":92},">) {",[86,869,870,872,874,876,878,880,882,884,886,888,890,892,895,897,899,901,904,906,908,910],{"class":88,"style":89},[86,871,538],{"style":96},[86,873,541],{"style":92},[86,875,97],{"style":96},[86,877,546],{"style":92},[86,879,187],{"style":96},[86,881,777],{"style":103},[86,883,178],{"style":92},[86,885,276],{"style":103},[86,887,146],{"style":96},[86,889,560],{"style":103},[86,891,178],{"style":92},[86,893,894],{"style":206},"\"chat:left\"",[86,896,264],{"style":92},[86,898,570],{"style":103},[86,900,573],{"style":92},[86,902,903],{"style":206},"\"id\"",[86,905,349],{"style":96},[86,907,546],{"style":92},[86,909,187],{"style":96},[86,911,912],{"style":92},"id})));",[86,914,915],{"class":88,"style":89},[86,916,419],{"style":92},[86,918,919],{"class":88,"style":89},[86,920,360],{"style":92},[86,922],{"class":88,"style":89},[86,924,925,928,930,933,935,938],{"class":88,"style":89},[86,926,927],{"style":103},"submit_gateway!",[86,929,178],{"style":92},[86,931,932],{"style":103},"ChatGateway",[86,934,264],{"style":92},[86,936,937],{"style":206},"\"\u002Fws\u002Fchat\"",[86,939,210],{"style":92},[941,942,943,944,946],"h3",{"id":24},"The ",[53,945,261],{}," trait",[948,949,950,966],"table",{},[951,952,953],"thead",{},[954,955,956,960,963],"tr",{},[957,958,959],"th",{},"Method",[957,961,962],{},"Default",[957,964,965],{},"Called",[967,968,969,984,997,1009],"tbody",{},[954,970,971,977,981],{},[972,973,974],"td",{},[53,975,976],{},"path() -> &'static str",[972,978,979],{},[53,980,207],{},[972,982,983],{},"registration",[954,985,986,991,994],{},[972,987,988],{},[53,989,990],{},"handle_connection(&self, ctx: Arc\u003CWsContext>)",[972,992,993],{},"no-op",[972,995,996],{},"on connect",[954,998,999,1004,1006],{},[972,1000,1001],{},[53,1002,1003],{},"handle_message(&self, ctx, event, data)",[972,1005,993],{},[972,1007,1008],{},"per inbound message",[954,1010,1011,1016,1018],{},[972,1012,1013],{},[53,1014,1015],{},"handle_disconnect(&self, ctx)",[972,1017,993],{},[972,1019,1020],{},"on disconnect",[50,1022,1023,1026,1027,1029],{},[53,1024,1025],{},"submit_gateway!(Type, \"\u002Fpath\")"," registers the gateway into the inventory so ",[53,1028,175],{}," can find it.",[67,1031,28],{"id":27},[50,1033,1034,1035,349],{},"Messages are JSON envelopes with an optional ",[53,1036,560],{},[70,1038,1040],{"language":1039,"class":81},"json",[53,1041,1043],{"class":1042},"language-json",[86,1044,1045,1048,1051,1054,1056,1058,1060,1063,1065,1067,1070,1072,1075,1077,1080],{"class":88,"style":89},[86,1046,1047],{"style":92},"{ ",[86,1049,1050],{"style":403},"\"event\"",[86,1052,1053],{"style":92},": ",[86,1055,745],{"style":206},[86,1057,264],{"style":92},[86,1059,762],{"style":403},[86,1061,1062],{"style":92},": { ",[86,1064,754],{"style":403},[86,1066,1053],{"style":92},[86,1068,1069],{"style":206},"\"...\"",[86,1071,264],{"style":92},[86,1073,1074],{"style":403},"\"text\"",[86,1076,1053],{"style":92},[86,1078,1079],{"style":206},"\"hi\"",[86,1081,1082],{"style":92}," } }",[70,1084,1085],{"language":80,"class":81},[53,1086,1087,108,1111,108,1136,108,1151],{"class":84},[86,1088,1089,1092,1095,1097,1100,1103,1105,1108],{"class":88,"style":89},[86,1090,1091],{"style":103},"IncomingMessage",[86,1093,1094],{"style":92}," { event",[86,1096,349],{"style":96},[86,1098,1099],{"style":103}," String",[86,1101,1102],{"style":92},", data",[86,1104,349],{"style":96},[86,1106,1107],{"style":103}," Value",[86,1109,1110],{"style":92}," }",[86,1112,1113,1115,1117,1119,1121,1124,1126,1128,1130,1133],{"class":88,"style":89},[86,1114,276],{"style":103},[86,1116,146],{"style":96},[86,1118,560],{"style":103},[86,1120,178],{"style":92},[86,1122,1123],{"style":206},"\"chat:history\"",[86,1125,264],{"style":92},[86,1127,570],{"style":103},[86,1129,178],{"style":92},[86,1131,1132],{"style":96},"...",[86,1134,1135],{"style":92},"))",[86,1137,1138,1140,1142,1145,1148],{"class":88,"style":89},[86,1139,276],{"style":103},[86,1141,146],{"style":96},[86,1143,1144],{"style":103},"from",[86,1146,1147],{"style":92},"(value)          ",[86,1149,1150],{"style":129},"\u002F\u002F event-less",[86,1152,1153,1155,1157,1159,1161,1164],{"class":88,"style":89},[86,1154,276],{"style":103},[86,1156,146],{"style":96},[86,1158,1144],{"style":103},[86,1160,178],{"style":92},[86,1162,1163],{"style":206},"\"plain string\"",[86,1165,228],{"style":92},[67,1167,1168],{"id":30},[53,1169,31],{},[50,1171,1172],{},"Per-connection context:",[70,1174,1175],{"language":80,"class":81},[53,1176,1177,108,1190,108,1202,108,1214,108,1229,108,1231,108,1250],{"class":84},[86,1178,1179,1182,1184,1187],{"class":88,"style":89},[86,1180,1181],{"style":92},"ctx",[86,1183,187],{"style":96},[86,1185,1186],{"style":92},"id            ",[86,1188,1189],{"style":129},"\u002F\u002F SocketId (Uuid)",[86,1191,1192,1194,1196,1199],{"class":88,"style":89},[86,1193,1181],{"style":92},[86,1195,187],{"style":96},[86,1197,1198],{"style":92},"path          ",[86,1200,1201],{"style":129},"\u002F\u002F connected path",[86,1203,1204,1206,1208,1211],{"class":88,"style":89},[86,1205,1181],{"style":92},[86,1207,187],{"style":96},[86,1209,1210],{"style":92},"query         ",[86,1212,1213],{"style":129},"\u002F\u002F query string",[86,1215,1216,1218,1220,1223,1226],{"class":88,"style":89},[86,1217,1181],{"style":92},[86,1219,187],{"style":96},[86,1221,1222],{"style":103},"server",[86,1224,1225],{"style":92},"()      ",[86,1227,1228],{"style":129},"\u002F\u002F handle to the WsServer",[86,1230],{"class":88,"style":89},[86,1232,1233,1235,1237,1239,1242,1244,1247],{"class":88,"style":89},[86,1234,1181],{"style":92},[86,1236,187],{"style":96},[86,1238,551],{"style":103},[86,1240,1241],{"style":92},"(msg)",[86,1243,163],{"style":96},[86,1245,1246],{"style":92},";        ",[86,1248,1249],{"style":129},"\u002F\u002F send to this socket",[86,1251,1252,1254,1256,1258,1260,1262,1265],{"class":88,"style":89},[86,1253,1181],{"style":92},[86,1255,187],{"style":96},[86,1257,777],{"style":103},[86,1259,1241],{"style":92},[86,1261,163],{"style":96},[86,1263,1264],{"style":92},";   ",[86,1266,1267],{"style":129},"\u002F\u002F send to all sockets",[67,1269,1270,1273],{"id":33},[53,1271,1272],{},"WsServer"," — rooms & broadcast",[50,1275,1276],{},"The global singleton server:",[948,1278,1279,1288],{},[951,1280,1281],{},[954,1282,1283,1285],{},[957,1284,959],{},[957,1286,1287],{},"Purpose",[967,1289,1290,1300,1310,1320,1330],{},[954,1291,1292,1297],{},[972,1293,1294],{},[53,1295,1296],{},"count() -> usize",[972,1298,1299],{},"Connected sockets",[954,1301,1302,1307],{},[972,1303,1304],{},[53,1305,1306],{},"send_to(id, msg)",[972,1308,1309],{},"Direct message",[954,1311,1312,1317],{},[972,1313,1314],{},[53,1315,1316],{},"broadcast(msg)",[972,1318,1319],{},"All sockets",[954,1321,1322,1327],{},[972,1323,1324],{},[53,1325,1326],{},"emit_to_room(room, msg)",[972,1328,1329],{},"One room",[954,1331,1332,1341],{},[972,1333,1334,1337,1338],{},[53,1335,1336],{},"join_room(id, room)"," \u002F ",[53,1339,1340],{},"leave_room(id, room)",[972,1342,1343],{},"Room membership",[70,1345,1346],{"language":80,"class":81},[53,1347,1348,108,1370],{"class":84},[86,1349,1350,1352,1354,1357,1360,1362,1365,1368],{"class":88,"style":89},[86,1351,1222],{"style":92},[86,1353,187],{"style":96},[86,1355,1356],{"style":103},"join_room",[86,1358,1359],{"style":92},"(ctx",[86,1361,187],{"style":96},[86,1363,1364],{"style":92},"id, ",[86,1366,1367],{"style":206},"\"general\"",[86,1369,210],{"style":92},[86,1371,1372,1374,1376,1379,1381,1383,1385,1387,1389,1391,1393,1396,1398,1400,1402,1405,1407,1410],{"class":88,"style":89},[86,1373,1222],{"style":92},[86,1375,187],{"style":96},[86,1377,1378],{"style":103},"emit_to_room",[86,1380,178],{"style":92},[86,1382,1367],{"style":206},[86,1384,264],{"style":92},[86,1386,276],{"style":103},[86,1388,146],{"style":96},[86,1390,560],{"style":103},[86,1392,178],{"style":92},[86,1394,1395],{"style":206},"\"room:update\"",[86,1397,264],{"style":92},[86,1399,570],{"style":103},[86,1401,573],{"style":92},[86,1403,1404],{"style":206},"\"n\"",[86,1406,349],{"style":96},[86,1408,1409],{"style":403}," 2",[86,1411,584],{"style":92},[67,1413,37],{"id":36},[50,1415,1416,1418,1419,264,1422,264,1425,1428,1429,1432,1433,1436],{},[53,1417,271],{}," covers ",[53,1420,1421],{},"Protocol(String)",[53,1423,1424],{},"Json(serde_json::Error)",[53,1426,1427],{},"Closed",", and ",[53,1430,1431],{},"Gateway(String)",", with a ",[53,1434,1435],{},"From\u003Caxum::Error>"," conversion.",[67,1438,40],{"id":39},[50,1440,1441,1442,1445],{},"The repo ships a full ",[53,1443,1444],{},"mono-ws-chat"," example (port 3003) with a chat gateway, history, rooms, and an e2e test.",[67,1447,43],{"id":42},[1449,1450,1451,1459],"ul",{},[1452,1453,1454,187],"li",{},[1455,1456,1458],"a",{"href":1457},"\u002Fdocs\u002Ftechniques\u002Fgrpc","gRPC",[1452,1460,1461,187],{},[1455,1462,1464],{"href":1463},"\u002Fdocs\u002Ftechniques\u002Fmessaging","Messaging",{"title":12,"searchDepth":13,"depth":14,"links":1466},[1467,1468,1471,1472,1473,1474,1475,1476],{"id":17,"depth":13,"text":18},{"id":20,"depth":13,"text":21,"children":1469},[1470],{"id":24,"depth":14,"text":25},{"id":27,"depth":13,"text":28},{"id":30,"depth":13,"text":31},{"id":33,"depth":13,"text":34},{"id":36,"depth":13,"text":37},{"id":39,"depth":13,"text":40},{"id":42,"depth":13,"text":43},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Ftechniques\u002Fwebsockets.md",{"title":1479,"slug":1480},"Rate limiting","techniques\u002Frate-limiting",{"title":1458,"slug":1482},"techniques\u002Fgrpc",1789565439199]