{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","settings":"","params":[],"results":{"codes":[]}},"next":{"description":"","pages":[]},"title":"WebSockets","type":"basic","slug":"websockets","excerpt":"","body":"WebSockets provide two way communication between the client and the server.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Route\"\n}\n[/block]\nCreating a socket is easy and supports the same type-safe routing as all other routes.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"app.socket(\\\"socket\\\") { req, ws in\\n\\n    ws.onText = { ws, text in\\n    \\t\\tapp.console(\\\"Socket received data: \\\\(text)\\\")\\n    }\\n\\n    ws.onClose = { ws, _, _, _ in\\n\\t\\t\\t\\tapp.console(\\\"Socket closed\\\")\\n    }\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nSocks run on `GET` methods.\n\nVisit `ws://<host>:<port>/socket` with a client that supports WebSockets.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Type Safe\"\n}\n[/block]\nAn example of using a socket with a type safe, StringInitializable `User`.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"app.socket(\\\"socket\\\", User.self) { req, ws, user in\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Socket Chat\"\n}\n[/block]\nTo view examples of a Socket Chat server and iOS app, visit our GitHub.\n\n[Socket Chat Server](https://github.com/qutheory/vapor-chat)\n[Socket Chat iOS App](https://github.com/qutheory/vapor-chat-ios)","updates":[],"order":999,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"57687991d98cfd0e006761d1","project":"57086fe855a4690e005de407","user":"57086f3d447c410e00221bd1","githubsync":"","version":{"version":"0.12","version_clean":"0.12.0","codename":"Client Clyde","is_stable":false,"is_beta":true,"is_hidden":false,"is_deprecated":true,"categories":["5768701b72cd4c0e000fad19","5768701b72cd4c0e000fad1a","5768701b72cd4c0e000fad1b","5768701b72cd4c0e000fad1c"],"_id":"5768701b72cd4c0e000fad18","releaseDate":"2016-06-20T22:37:15.641Z","__v":1,"createdAt":"2016-06-20T22:37:15.641Z","project":"57086fe855a4690e005de407"},"__v":3,"category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"Guide","slug":"getting-started","order":1,"from_sync":false,"reference":false,"_id":"5768701b72cd4c0e000fad1a","createdAt":"2016-04-09T04:31:52.489Z","__v":0,"version":"5768701b72cd4c0e000fad18","project":"57086fe855a4690e005de407"},"createdAt":"2016-06-20T23:17:37.675Z"}
WebSockets provide two way communication between the client and the server. [block:api-header] { "type": "basic", "title": "Route" } [/block] Creating a socket is easy and supports the same type-safe routing as all other routes. [block:code] { "codes": [ { "code": "app.socket(\"socket\") { req, ws in\n\n ws.onText = { ws, text in\n \t\tapp.console(\"Socket received data: \\(text)\")\n }\n\n ws.onClose = { ws, _, _, _ in\n\t\t\t\tapp.console(\"Socket closed\")\n }\n}", "language": "swift" } ] } [/block] Socks run on `GET` methods. Visit `ws://<host>:<port>/socket` with a client that supports WebSockets. [block:api-header] { "type": "basic", "title": "Type Safe" } [/block] An example of using a socket with a type safe, StringInitializable `User`. [block:code] { "codes": [ { "code": "app.socket(\"socket\", User.self) { req, ws, user in", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Socket Chat" } [/block] To view examples of a Socket Chat server and iOS app, visit our GitHub. [Socket Chat Server](https://github.com/qutheory/vapor-chat) [Socket Chat iOS App](https://github.com/qutheory/vapor-chat-ios)