### Server Example: Echo Server Source: https://rubydoc.info/gems/eventmachine A basic example demonstrating how to set up a simple echo server using EventMachine. This server listens for incoming connections and echoes back any data received. ```APIDOC ## Server Example: Echo Server ### Description This example demonstrates a basic echo server using EventMachine. It listens on a specified port and echoes back any data received from clients. ### Code ```ruby require 'eventmachine' class EchoServer < EM::Connection def receive_data(data) send_data data end end EM.run { EM.start_server '127.0.0.1', 8080, EchoServer puts 'Echo server started on port 8080' } ``` ### How to Run 1. Save the code as `echo_server.rb`. 2. Run the script using `ruby echo_server.rb`. 3. Connect to `127.0.0.1:8080` using a tool like `telnet` or `netcat` and send data. ``` -------------------------------- ### Install EventMachine Gem Source: https://rubydoc.info/gems/eventmachine Install the EventMachine gem using RubyGems. ```bash gem install eventmachine ``` -------------------------------- ### EventMachine Echo Server Example Source: https://rubydoc.info/gems/eventmachine A fully-functional echo server written with EventMachine. This example demonstrates basic connection handling, data reception, and sending data back to the client. It will block the current thread. ```ruby require 'eventmachine' module EchoServer def post_init puts "-- someone connected to the echo server!" end def receive_data data send_data ">>>you sent: #{data}" close_connection if data =~ /quit/i end def unbind puts "-- someone disconnected from the echo server!" end end # Note that this will block current thread. EventMachine.run { EventMachine.start_server "127.0.0.1", 8081, EchoServer } ``` -------------------------------- ### Add EventMachine to Gemfile Source: https://rubydoc.info/gems/eventmachine Add the EventMachine gem to your Gemfile if you are using Bundler. ```ruby gem "eventmachine" ``` -------------------------------- ### What is EventMachine Source: https://rubydoc.info/gems/eventmachine EventMachine is an event-driven I/O and lightweight concurrency library for Ruby. It utilizes the Reactor pattern for event-driven I/O, similar to libraries like Node.js and Python's Twisted. Its primary goals are to provide extreme scalability, performance, and stability while simplifying the complexities of threaded network programming. ```APIDOC ## What is EventMachine ### Description EventMachine is an event-driven I/O and lightweight concurrency library for Ruby that uses the Reactor pattern. It is designed for high scalability, performance, and stability, while simplifying network programming. ### Use Cases - Scalable event-driven servers (e.g., Thin, Goliath) - Asynchronous clients for protocols and APIs (e.g., em-http-request, amqp) - Efficient network proxies - File and network monitoring tools ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.