From f258f28689a26a476aaf265e5ab8c71b2890bcce Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Wed, 16 Dec 2015 20:19:30 +0200 Subject: [PATCH] Add websocket doc --- docs/websockets.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 docs/websockets.md diff --git a/docs/websockets.md b/docs/websockets.md new file mode 100644 index 0000000..630505c --- /dev/null +++ b/docs/websockets.md @@ -0,0 +1,35 @@ +# Using Websockets + +Using Websockets is super easy! By nature Websockets are a bit different than standard Http Request/Response lifecycle. + +You can easily create a websocket handler which matches the route of `ws://host:port/route. You can create more than 1 websocket handler +with different routes. + +```ruby + ws "/" do |socket| + + end + + ws "/route2" do |socket| + + end +``` + +Let's access the socket and create a simple echo server. + +```ruby + ws "/" do |socket| + # Send welcome message to the client + socket.send "Hello from Kemal!" + + # Handle incoming message and echo back to the client + socket.on_message do |message| + socket.send "Echo back from server #{message}" + end + + # Executes when the client is disconnected. You can do the cleaning up here. + socket.on_close do + puts "Closing socket" + end + end +```