SlideShare a Scribd company logo
Encheção de linguiça :D




                          @dev_ton
Olá!




       @dev_ton
eu

Antônio Roberto ( ton )
e você




                          @dev_ton
2009


       @dev_ton
@dev_ton
Leroy




eu




             @dev_ton
@dev_ton
@dev_ton
apoio



        @dev_ton
Introdução ao
CRAMP e WebSockets

       apoio



                     @dev_ton
CRAMP



        @dev_ton
CRAMP

- O que é?
- Quando usar?




                         @dev_ton
CRAMP
Controller & Model




                     @dev_ton
CRAMP
Cramp::Controller

Os 4 estágios após uma requisição:
Initialization
Response Initialization
Starting
Finishing




                                     @dev_ton
CRAMP
Cramp::Controller
Helpers:
on_start
before_start
on_finish
periodic_timer
keep_connection_alive
respond_with
request_limit_exceeded




                                 @dev_ton
CRAMP
Cramp::Controller
on_start

 class Hello < Cramp::Controller::Action
   on_start :say_hello

   def say_hello
     render "Hello World! :)"
     finish
   end
 end



                                           @dev_ton
CRAMP
Cramp::Controller
before_start

 before_start :valid_param

 def valid_user
   if param[:id] =~ /[^a-zA-Z](.*)/
     yield
   else
     halt 500, {'Content-Type' => 'text/plain'}, "Invalid ID"
   end
 end


                                                                @dev_ton
CRAMP
Cramp::Controller
on_finish
 class PollUserAction < Cramp::Controller::Action
   on_start :start_polling
   on_finish :stop_polling

   def start_polling
     # Call find_user every 10 seconds
     @timer = EventMachine::PeriodicTimer.new(2) { find_user }
   end

   def find_user
     User.first {|u| render "#{u.inspect}n" }
   end

   def stop_polling
     puts "Cancelling the timer.."
     @timer.cancel
   end
 end



                                                                 @dev_ton
CRAMP
Cramp::Controller
periodic_timer

 class PollUserAction < Cramp::Controller::Action
   periodic_timer :poll_user, :every => 2

   def poll_user
     User.first {|u| render "#{u.inspect}n" }
   end
 end



                                                    @dev_ton
CRAMP
Cramp::Controller
keep_connection_alive


 class PollUserAction < Cramp::Controller::Action
   periodic_timer :poll_user, :every => 2
   keep_connection_alive, :every => 30
 end




                                                    @dev_ton
CRAMP
Cramp::Controller
respond_with


 def respond_with
   content_type = params[:format] == 'xml' ? 'application/xml' : 'application/json'
   [200, {'Content-Type' => content_type}]
 end




                                                                                 @dev_ton
CRAMP
Cramp::Controller
request_limit_exceeded
 class PollUserAction < Cramp::Controller::Action
   periodic_timer :poll_user, :every => 2
   periodic_timer :check_limit_exceed, :every => 10

   def poll_user
     ..
   end

   def check_limit_exceed
     finish if request_limit_exceeded
   end
 end


                                                      @dev_ton
CRAMP
Cramp::Model




                       @dev_ton
CRAMP
Cramp::Model




                :(
                       @dev_ton
CRAMP
Cramp::Model

Cramp::Model.init(:username => 'root', :database =>
'cramp_development')

class User < Cramp::Model::Base
  attribute :id, :type => Integer, :primary_key => true
  attribute :name

  validates_presence_of :name
end




                                                          @dev_ton
CRAMP
Cramp::Model

EM.run do
  User.select(:id, :name).limit(10).offset(20).first {|u| .. }
  User.where(User[:name].eq('Lush')).limit(2).all {|users| ... }
  User.each {|user| .. }

  def lush_users_found(users)
    ...
  end

  User.where(User[:name].eq('Lush'), User[:id].gt(5)).all
method(:lush_users_found)
end




                                                                   @dev_ton
CRAMP
Cramp::Model

EM.run do
  def user_saved(status)
    if status.success?
      ...
    else
      user = status.record
      puts "Oops. Found errors : #{status.record.errors.inspect}"
    end
  end

  user = User.new
  user.save method(:user_saved)
end




                                                                    @dev_ton
WebSocket



            @dev_ton
WebSocket
  A mágica!




              @dev_ton
WebSocket
                              4 passos

var location = 'http://localhost:8080/backend';
var socket = new WebSocket(location);

socket.onopen = function(data) { ... };

socket.onmessage = function(data) {... };

socket.onclose = function(data) { ... };

socket.send(data);


https://gist.github.com/98c8448ba054b7992aff#file_example.js



                                                              @dev_ton
WebSocket
   &
 CRAMP


            @dev_ton
WebSocket & CRAMP
Cramp::Controller::Websocket


Cramp::Controller::Websocket.backend = :thin



on_data




                                               @dev_ton
WebSocket & CRAMP
on_data

 Cramp::Controller::Websocket.backend = :thin

 class WelcomeController < Cramp::Controller::Websocket
   on_data :received_data

   def received_data(data)
     render "Got your #{data}"
   end
 end


https://gist.github.com/d04affd72e2ea57039eb




                                                          @dev_ton
Ajax & WebSockets




                    @dev_ton
DEMO :D



          @dev_ton
Obrigado! :)

email: forevertonny@gmail.com
twitter: @dev_ton
links
Indrodução ao CRAMP & WebSockets ( patrik )
http://m.onkey.org/2010/1/7/introducing-cramp
http://m.onkey.org/2010/1/15/websockets-made-easy-with-cramp

WebSockets API
http://dev.w3.org/html5/websockets/

Video ( pubsubhubbub )
http://www.youtube.com/watch?v=B5kHx0rGkec

App Apresentação
http://github.com/devton/socketchat




                                                               @dev_ton
Obrigado!




            @dev_ton

More Related Content

What's hot (20)

DOCX
Getting Started with Maven and Cucumber in Eclipse
Tom Arend
 
PPT
Write book in markdown
Larry Cai
 
DOCX
Installing symfony within netbeans and WAMP
Riyad Bin Zaman
 
PDF
Symfony2 Components - The Event Dispatcher
Sarah El-Atm
 
PDF
Asynchronous Programming FTW! 2 (with AnyEvent)
xSawyer
 
PPT
GTLAB Overview
marpierc
 
PPT
Php Debugger
guest8cd374
 
PDF
No Hugging, No Learning
Olaf Alders
 
KEY
Speedy TDD with Rails
PatchSpace Ltd
 
PDF
Load Testing with PHP and RedLine13
Jason Lotito
 
PDF
Intro to testing Javascript with jasmine
Timothy Oxley
 
PPT
Real Time Event Dispatcher
Peter Dietrich
 
PDF
Quick tour to front end unit testing using jasmine
Gil Fink
 
PDF
Advanced Jasmine - Front-End JavaScript Unit Testing
Lars Thorup
 
PPTX
Design patterns as power of programing
Lukas Lesniewski
 
PDF
Flask With Server-Sent Event
Tencent
 
PDF
Unlock The Mystery Of PHPUnit (Wave PHP 2018)
ENDelt260
 
PDF
WP Weekend #2 - Corcel, aneb WordPress přes Laravel
Brilo Team
 
DOC
Debugging over tcp and http
Kaniska Mandal
 
PPTX
Java do-while Loop
Rhythm Suiwal
 
Getting Started with Maven and Cucumber in Eclipse
Tom Arend
 
Write book in markdown
Larry Cai
 
Installing symfony within netbeans and WAMP
Riyad Bin Zaman
 
Symfony2 Components - The Event Dispatcher
Sarah El-Atm
 
Asynchronous Programming FTW! 2 (with AnyEvent)
xSawyer
 
GTLAB Overview
marpierc
 
Php Debugger
guest8cd374
 
No Hugging, No Learning
Olaf Alders
 
Speedy TDD with Rails
PatchSpace Ltd
 
Load Testing with PHP and RedLine13
Jason Lotito
 
Intro to testing Javascript with jasmine
Timothy Oxley
 
Real Time Event Dispatcher
Peter Dietrich
 
Quick tour to front end unit testing using jasmine
Gil Fink
 
Advanced Jasmine - Front-End JavaScript Unit Testing
Lars Thorup
 
Design patterns as power of programing
Lukas Lesniewski
 
Flask With Server-Sent Event
Tencent
 
Unlock The Mystery Of PHPUnit (Wave PHP 2018)
ENDelt260
 
WP Weekend #2 - Corcel, aneb WordPress přes Laravel
Brilo Team
 
Debugging over tcp and http
Kaniska Mandal
 
Java do-while Loop
Rhythm Suiwal
 

Viewers also liked (16)

DOCX
Care and Nutrition for a Sick Child
Salim e-a Ebrahim
 
PPT
Scald injury powerpointpresentation
Ashland KY Fire Dept
 
PPTX
Nutrition in sick children
Dr Bedangshu Saikia
 
PPTX
Athletes nutrition and optimum performance
c3162739
 
PPTX
Food, nutrition and exercise
Rasheed Perry
 
PPT
Child nutrition
FåhMîdä SüPtî
 
PPTX
Land disposal of waste and Environmental pollution
Chanuk Liyanage
 
PDF
Sports nutrition
fitnesslex
 
PDF
Sports Injuries
Carmela Domocmat
 
PPT
Strain sprain fracture
mae2388
 
PPTX
fracture and dislocation ppt . Almas khan. khorfakkhan hospital dubai
almasmkm
 
PPTX
Poisoning
Beka Aberra
 
PPTX
Methods of waste disposal reduce, reuse , recovery, recycle
praison
 
PPT
Burns
shabeel pn
 
PPTX
Waste disposal
INAAMUL HAQ
 
PPT
Wound Healing & Wound Care
Dr. Souvik Adhikari
 
Care and Nutrition for a Sick Child
Salim e-a Ebrahim
 
Scald injury powerpointpresentation
Ashland KY Fire Dept
 
Nutrition in sick children
Dr Bedangshu Saikia
 
Athletes nutrition and optimum performance
c3162739
 
Food, nutrition and exercise
Rasheed Perry
 
Child nutrition
FåhMîdä SüPtî
 
Land disposal of waste and Environmental pollution
Chanuk Liyanage
 
Sports nutrition
fitnesslex
 
Sports Injuries
Carmela Domocmat
 
Strain sprain fracture
mae2388
 
fracture and dislocation ppt . Almas khan. khorfakkhan hospital dubai
almasmkm
 
Poisoning
Beka Aberra
 
Methods of waste disposal reduce, reuse , recovery, recycle
praison
 
Burns
shabeel pn
 
Waste disposal
INAAMUL HAQ
 
Wound Healing & Wound Care
Dr. Souvik Adhikari
 
Ad

Similar to Cramp websockets (20)

KEY
Código Saudável => Programador Feliz - Rs on Rails 2010
Plataformatec
 
PDF
Background Jobs - Com BackgrounDRb
Juan Maiz
 
KEY
How To Test Everything
noelrap
 
PDF
TypeScript for Java Developers
Yakov Fain
 
KEY
Aprendendo solid com exemplos
vinibaggio
 
PDF
Intro To JavaScript Unit Testing - Ran Mizrahi
Ran Mizrahi
 
PDF
Djangocon 2014 angular + django
Nina Zakharenko
 
PDF
TDC 2015 - DSLs em Ruby
Guilherme Carreiro
 
PDF
Testing Legacy Rails Apps
Rabble .
 
PPT
Capistrano Overview
Travis Roberts
 
PDF
My name is Trinidad
David Calavera
 
PDF
Using Sinatra to Build REST APIs in Ruby
LaunchAny
 
PDF
Django tricks (2)
Carlos Hernando
 
PDF
Puppet Camp DC 2015: Distributed OpenSCAP Compliance Validation with MCollective
Puppet
 
PPTX
Joker 2015 - Валеев Тагир - Что же мы измеряем?
tvaleev
 
KEY
SOLID Ruby, SOLID Rails
Jens-Christian Fischer
 
KEY
Dancing with websocket
Damien Krotkine
 
PPT
My programming final proj. (1)
aeden_brines
 
PDF
Palestra "Do PHP ao Rails" - FOCAI
Almir Mendes
 
PDF
Complex Made Simple: Sleep Better with TorqueBox
bobmcwhirter
 
Código Saudável => Programador Feliz - Rs on Rails 2010
Plataformatec
 
Background Jobs - Com BackgrounDRb
Juan Maiz
 
How To Test Everything
noelrap
 
TypeScript for Java Developers
Yakov Fain
 
Aprendendo solid com exemplos
vinibaggio
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Ran Mizrahi
 
Djangocon 2014 angular + django
Nina Zakharenko
 
TDC 2015 - DSLs em Ruby
Guilherme Carreiro
 
Testing Legacy Rails Apps
Rabble .
 
Capistrano Overview
Travis Roberts
 
My name is Trinidad
David Calavera
 
Using Sinatra to Build REST APIs in Ruby
LaunchAny
 
Django tricks (2)
Carlos Hernando
 
Puppet Camp DC 2015: Distributed OpenSCAP Compliance Validation with MCollective
Puppet
 
Joker 2015 - Валеев Тагир - Что же мы измеряем?
tvaleev
 
SOLID Ruby, SOLID Rails
Jens-Christian Fischer
 
Dancing with websocket
Damien Krotkine
 
My programming final proj. (1)
aeden_brines
 
Palestra "Do PHP ao Rails" - FOCAI
Almir Mendes
 
Complex Made Simple: Sleep Better with TorqueBox
bobmcwhirter
 
Ad

Recently uploaded (20)

PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
introduction to computer hardware and sofeware
chauhanshraddha2007
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
PDF
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PPTX
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PPTX
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PPTX
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
introduction to computer hardware and sofeware
chauhanshraddha2007
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
The Future of Artificial Intelligence (AI)
Mukul
 
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 

Cramp websockets