Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Phoenix Mongo Users Group - ReactiveMongo

Phoenix Mongo Users Group - ReactiveMongo

Overview of ReactiveMongo - an asynchronous driver for MongoDB written in Scala. Covers the basics of using the driver as well as the Play ReactiveMongo plugin via a sample application. Covers ReactiveMongo 0.9 and Play Framework 2.1.

Avatar for Terry Drozdowski

Terry Drozdowski

September 05, 2013
Tweet

More Decks by Terry Drozdowski

Other Decks in Programming

Transcript

  1. ReactiveMongo - Agenda • Introduction to ReactiveMongo • Core •

    Play-ReactiveMongo (Play Framework Plugin) • Scala Overview • Examples • Core API Usage • Play-ReactiveMongo • MongoController • CRUD • GridFS • Tailable Cursors • Real Life Example : Exceda Portal • ReactiveMongo Roadmap • Q&A
  2. ReactiveMongo - Overview • Asynchronous, pure-Scala driver for MongoDB •

    'Reactive' - new buzzword • Progressive Stream Processing • No blocking/polling • Written on top of Akka • Driver is composed of an ActorSystem and various Actors • http://reactivemongo.org/
  3. ReactiveMongo - Components • Composed of various components • ReactiveMongo

    BSON • Core BSON Library for the project • Can be used separately - only dependency is Scala itself • ReactiveMongo BSON Macros • Uses compile-time macros to introspect your classes and generate your Readers/Writers on the fly • Similar to Play 2.1 JSON Macros • ReactiveMongo (core) • Plugin • Play-ReactiveMongo • Provides helpers for Play Applications • MongoController • Adds BSON -> JSON implicits • Provides JSONCollection
  4. ReactiveMongo - Mongo Scala Drivers • Official List: http://docs.mongodb.org/ecosystem/drivers/scala/ •

    Casbah • Officially Supported 'Driver' • Thin Wrapper over Java Driver (Currently Not Asynchronous) • Provides a rich DSL for composing Mongo Queries • HammerSmith • The first Asynchronous Mongo Driver for Scala • Based on Netty • ReactiveMongo • General Framework Supported Drivers • Lift Web Framework • Rogue (type-safe query DSL - from Foursquare) • BlueEyes • Frontlets
  5. ReactiveMongo - Scala Overview • Part of 'Java.next' (i.e. Groovy,

    Clojure, JRuby, etc) • Created by Martin Odersky in 2004 • Martin was involved with evolution of Java (i.e. Generics) • Mostly had academic use until recently • Hybrid Language: Functional & Object Oriented • Under Direction of Typesafe (http://typesafe.com) • Includes Odersky himself, as well as Rod Johnson and James Gosling as board members/Advisors • Source Code: https://github.com/scala/scala
  6. ReactiveMongo - Scala Overview • Used by a number of

    organizations/start ups in production today • Increased Developer Productivity • Scales Extremely Well • Twitter is best example • Easily Build Concurrent Code • Case Studies: http:// typesafe.com/company/ casestudies
  7. ReactiveMongo - Scala Overview • Language Features • Uses Java

    Virtual Machine (JVM) • Easy Interop with Java (bi-directional) • Command-line REPL • Everything is an Object (including Functions!) • Statically Typed • Keywords to identify singletons and immutable variables (val vs var) • Modern syntax - similar to Java/C and other {}'s - but lacks the verbosity (unless you want it) • Modern Language Features (for comprehensions, options, futures, actors, lazy initialization, etc) • No interfaces - 'traits' instead • aka 'mix-in' • Pattern Matching • High Order Functions (easily compose new functions from existing - with type safety)
  8. ReactiveMongo - Scala Cheat Sheet • Disclaimer: I am not

    a Scala black-belt (yet) :) • Cheat Sheet for some core ideas you'll see tonight: • val • keyword that defined an immutable variable • map • function on 'collections' and other 'special' classes (Future, Option and Try) that allow you to convert the value(s) within the said 'collection' to something else • Option • Indicates a function returned either Some(x) or None (something or nothing) • Future • Contains a Promise that holds a reference to a value you're going to receive...eventually • Try • Indicates that the results of a function was either a Success(x) or Failure(exception) • '_' • Pattern matcher for anything, similar to '*' in other languages
  9. ReactiveMongo - Playframework Overview • Scala web application framework from

    Typesafe • Lightweight • No JEE here! • Built on Netty (future versions to be built on akka-io) • Command-line tools to generate project scaffolding and manage build/deploy • leverages sbt under the hood • Powerful, type-safe JSON library • Iteratee/Enumeratee
  10. ReactiveMongo - Iteratee/Enumeratee • Allow developers to create, consume and

    transform streams of data • All data sources are handled the same (files, websockets, etc) • Asynchronous/Non-blocking • Can be used w/o Play Framework (stand alone lib) • sbt: "play" %% "play-iteratees" % "2.1.3" • http://www.playframework.com/documentation/2.1.x/Iteratees
  11. ReactiveMongo - Iteratee/Enumeratee • Iteratee • consumer (or sink), describes

    the way input will be consumed to produce a given value • ie. Iteratee[String,Int] - consumes a String and returns an Int • states • Cont - able to process input • Error - error state • Done - completed stream processing • Enumerator • Pushes data into the Iteratee; producer • Returns the state of the Iteratee when completed • Enumeratee • adapts and transforms stream of data • Enumeratee[From, To] • used by Iteratee
  12. ReactiveMongo - Examples • Core (https://gist.github.com/tdrozdowski/056744d6ecf6707b29c3) • Demonstrates basic use

    of the Core library (BSONDocuments, queries and use of an Iteratee to read results) • Play Plugin (https://gist.github.com/tdrozdowski/4451005fb1f31c730f75) • Configuration • How to set this up properly • MongoController • Demonstrates use of the JSONCollection • GridFS • Demonstrates use of the GridFS BodyParser • Tailable Cursor • Demonstrates use of Play's WebSocket support with ReactiveMongo's tailable cursor support. • GitHub Code: • reactivePlay ( https://github.com/tdrozdowski/reactivePlay ) • reactiveWeb ( https://github.com/tdrozdowski/reactiveWeb )
  13. ReactiveMongo - Real Life Example: Exceda Portal • Architecture •

    Buzzword Bingo: PlayFramework, AngularJS, CoffeeScript, MongoDB, Akka, NodeJS • Core languages: Scala/CoffeeScript • Build/Workflow Mgmt: Yeoman/Play • Cloud Deployment Platforms: Openshift/MongoHQ • Content Delivery Network (CDN): Akamai • Cloud Integrations • Akamai (SOAP - ugh) • Chargify (REST) • ZenDesk (REST) • JSON 'Coast to Coast' • Use of MongoController • Use of DAO 'pattern' with Actors
  14. ReactiveMongo - Roadmap • 0.9 is 'stable' version • Version

    used in samples tonight • 0.10-SNAPSHOT is 'bleeding edge' • contains some API changes :( • Upgrade to Akka 2.2 • 1.0 • Performance Enhancements • Command Refactoring (make them more generic - like collections are) • Improved Authentication API • Bug Fixes • beyond... • remove Netty entirely and build upon Akka-io • Allow for integration with other 'useful' libraries such as: • Rogue • Casbah Query DSL