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

EventMachine

 EventMachine

Introduction to EventMachine given at Ruby::AZ

Chris Gaffney

January 10, 2011
Tweet

More Decks by Chris Gaffney

Other Decks in Programming

Transcript

  1. require "socket" server = TCPServer.new("0.0.0.0", 1845) loop do client =

    server.accept while line = client.gets client.write(line) end end
  2. require "socket" server = TCPServer.new("0.0.0.0", 1845) loop do client =

    server.accept while line = client.gets client.write(line) end end
  3. require "socket" server = TCPServer.new("0.0.0.0", 1845) loop do client =

    server.accept Thread.new do while line = client.gets client.write(line) end end end
  4. server = TCPServer.new(1845) clients = [] loop do ready =

    IO.select([server] + clients) ready[0].each do |socket| if socket == server clients << socket.accept_nonblock else data = socket.recv_nonblock(256) if data.size > 0 socket.sendmsg_nonblock(data) else clients.delete(socket) end end end end
  5. require 'rubygems' require 'eventmachine' module Server def receive_data(data) send_data(data) end

    end EventMachine.run do EventMachine.start_server('0.0.0.0', 1845, Server) end
  6. class Server < EventMachine::Connection # def post_init # def receive_data(data)

    # def unbind end EventMachine.run do EventMachine.start_server('127.0.0.1', 1845, Server) end
  7. class Client < EventMachine::Connection # def post_init # def connection_completed

    # def receive_data(data) # def unbind end EventMachine.run do EventMachine.connect('localhost', 1845, Client) EventMachine.connect('localhost', 1845, Client) end
  8. EventMachine .add_timer(delay, &block) .add_periodic_timer(delay, &block) .connect(bind, port, handler, *args) .start_server(bind,

    port, handler, *args) .run(&block) EventMachine::Connection #send_data(data) #close_connection(after_writing = false) #reconnect(server, port) Rdocs: http://rubyeventmachine.com/
  9. em-http-request EventMachine.run do query = { :lat => '37.704276', :lng

    => '-85.871227', :radius => 50 } head = { :accept => 'application/json' } http = EventMachine::HttpRequest.new("http://api.gowalla.com/spots"). get(:query => query, :head => head) http.callback do data = Yajl::Parser.parse(http.response) data['spots'].each do |spot| # American Legion Public Pool @ 37.705699083:-85.8540598 puts "#{spot['name']} @ #{spot['lat']}:#{spot['lng']}" end end end
  10. em-mongo EventMachine.run do mongo = EM::Mongo::Connection.new.db("demos") tweets = mongo.collection("tweets") tweets.insert({

    :user => 'gaffneyc', :text => 'Hello from Ruby::AZ'}) tweets.find(:user => 'gaffneyc') do |results| # [{ # "_id" => BSON::ObjectId('4d2a0037f50f833b19000001'), # "user" => "gaffneyc", # "text" => "Hello from Ruby::AZ" # }] end # tweets.remove(id) # tweets.update(conditions, updates) end
  11. em-redis EventMachine.run do redis = EM::Protocols::Redis.connect redis.set("test", "5000") do |_|

    redis.get("test") do |result| # "5000" puts result.inspect redis.delete("test") end end end Protip: Resque.redis = EM::Protocols::Redis.connect
  12. twitter-stream EventMachine.run do stream = ::Twitter::JSONStream.connect( :filters => "ruby,rails,collectiveidea", :auth

    => "[username:password]" ) stream.each_item do |item| tweet = Yajl::Parser.parse(item) # {"text"=>"So.. I just found out Justin Bieber's in the...} puts tweet.inspect end # stream.on_error # stream.on_max_reconnects end