Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6638545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:26:55+00:00 2026-05-25T23:26:55+00:00

I’ve crafted a basic TCP client using EventMachine. Code: # run.rb EventMachine::run do EventMachine::connect

  • 0

I’ve crafted a basic TCP client using EventMachine. Code:

# run.rb
EventMachine::run do
  EventMachine::connect $config_host, $config_port, Bot
end

# bot.rb
module Bot
  def post_init
    # log us in and do any other spinup.
    sleep(1)
    send_data $config_login + "\n"
    EventMachine.add_periodic_timer($config_keepalive_duration) { send_data $config_keepalive_str + "\n" }
    @valid_command =  /^<#{$config_passphrase}:([^>:]+):(#\d+)>(.*)$/
  end

  def receive_data(data)
    if(ma = @valid_command.match(data))
      command, user, args = ma[1,3]
      args.strip!
      command.downcase!
      p "Received: #{command}, #{user}, #{args}"
      # and.. here we handle the command.
    end
  end
end

This all works quite well. The basic idea is that it should connect, listen for specially formatted commands, and execute the command; in executing the command, there may be any number of “actions” taken that result in various data sent by the client.

But, for my next trick, I need to add the ability to actually handle the commands that Bot receives.

I’d like to do this using a dynamic library of event listeners, or something similar to that; ie, I have an arbitrary number of plugins that can register to listen for a specific command and get a callback from bot.rb. (Eventually, I’d like to be able to reload these plugins without restarting the bot, too.)

I’ve looked at the ruby_events gem and I think this makes sense but I’m having a little trouble of figuring out the best way to architect things. My questions include…

  1. I’m a little puzzled where to attach ruby_events listeners to – it just extends Object so it doesn’t make it obvious how to implement it.
  2. Bot is a module, so I can’t just call Bot.send_data from one of the plugins to send data – how can I interact with the EM connection from one of the plugins?

I’m completely open to any architectural revisions or suggestions of other gems that make what I’m trying to do easier, too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:26:55+00:00Added an answer on May 25, 2026 at 11:26 pm

    I’m not sure exactly what you’re trying to do, but one common pattern of doing this in EM is to define the command handlers as callbacks. So then the command logic can be pushed up out of the Bot module itself, which just handles the basic socket communication and command parsing. Think of how a web server dispatches to an application – the web server doesn’t do the work, it just dispatches.

    For example something like this

    EM::run do 
    
      bot = EM::connect $config_host, $config_port, Bot
    
      bot.on_command_1 do |user, *args|
         # handle command_1 in a block
      end
    
      # or if you want to modularize it, something like this
      # where Command2Handler = lambda {|user, *args| #... }
      bot.on_command_2(&Command2Handler)
    
    end
    

    So then you just need to implement #on_command_1, #on_command_2, etc. in your Bot, which is just a matter of storing the procs to instance vars; then calling them once you parse the passed commands out of your receive_data.

    A good, very readable example of this in production is TwitterStream.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.