Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
sgodbillon committed May 7, 2013
1 parent 1852b4a commit 46d96fd
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Tailable Cursors through WebSockets with ReactiveMongo and Play 2

ReactiveMongo allows to enumerate a MongoDB cursor in a non-blocking and asynchronous way, using Play's Iteratee library.
ReactiveMongo allows to enumerate a MongoDB cursor in a non-blocking and asynchronous way.

This demo shows how to stream the documents that are inserted into a capped collection through a WebSocket.
This demo shows how to stream the documents that are inserted into a capped collection through a WebSocket, using the [ReactiveMongo Play Plugin](https://github.com/zenexity/Play-ReactiveMongo).
It demonstrates the following features:
+ Tailable Cursors with ReactiveMongo
+ Creating capped collections
Expand All @@ -14,23 +14,34 @@ It demonstrates the following features:
When run, this sample allows to submit new documents via a WebSocket and inserts them into the Capped Collection. This Capped Collection is enumerated through the same Websocket, so any new document is eventually sent back to the client.

```scala
def watchCollection = WebSocket.using[JsValue] { request =>
val coll = ReactiveMongoPlugin.collection("acappedcollection")

// Inserts the received messages into the capped collection
val in = Iteratee.foreach[JsValue] { json =>
println("received " + json)
coll.insert(json)
}
// Enumerates the capped collection
val out = {
val cursor = coll.find(Json.obj(), QueryOpts().tailable.awaitData)
cursor.enumerate
}

// We're done!
(in, out)
def watchCollection = WebSocket.using[JsValue] { request =>
val collection = db.collection[JSONCollection]("acappedcollection")
// Inserts the received messages into the capped collection
val in = Iteratee.flatten(futureCollection.map(collection => Iteratee.foreach[JsValue] { json =>
println("received " + json)
collection.insert(json)
}))

// Enumerates the capped collection
val out = {
val futureEnumerator = futureCollection.map { collection =>
// so we are sure that the collection exists and is a capped one
val cursor: Cursor[JsValue] = collection
// we want all the documents
.find(Json.obj())
// the cursor must be tailable and await data
.options(QueryOpts().tailable.awaitData)
.cursor[JsValue]

// ok, let's enumerate it
cursor.enumerate
}
Enumerator.flatten(futureEnumerator)
}

// We're done!
(in, out)
}
```

Get the complete example and run it! It uses Play 2.1 master, ReactiveMongo master and Play ReactiveMongo Plugin.
Get the complete example and run it! It uses Play 2.1, ReactiveMongo master and Play ReactiveMongo Plugin.

0 comments on commit 46d96fd

Please sign in to comment.