These docs are for v0.12. Click to read the latest docs for v0.16.

WebSockets

WebSockets provide two way communication between the client and the server.

Route

Creating a socket is easy and supports the same type-safe routing as all other routes.

app.socket("socket") { req, ws in

    ws.onText = { ws, text in
    		app.console("Socket received data: \(text)")
    }

    ws.onClose = { ws, _, _, _ in
				app.console("Socket closed")
    }
}

Socks run on GET methods.

Visit ws://<host>:<port>/socket with a client that supports WebSockets.

Type Safe

An example of using a socket with a type safe, StringInitializable User.

app.socket("socket", User.self) { req, ws, user in

Socket Chat

To view examples of a Socket Chat server and iOS app, visit our GitHub.

Socket Chat Server
Socket Chat iOS App