I was trying out this code (got from an online article here: http://www.randomhacks.net/articles/2009/05/08/chat-client-ruby-amqp-eventmachine-shoes)
require 'rubygems'
gem 'amqp'
require 'mq'
unless ARGV.length == 2
STDERR.puts "Usage: #{$0} "
exit 1
end
$channel, $nick = ARGV
AMQP.start(:host => 'localhost') do
$chat = MQ.topic('chat')
# Print any messages on our channel.
queue = MQ.queue($nick)
queue.bind('chat', :key => $channel)
queue.subscribe do |msg|
if msg.index("#{$nick}:") != 0
puts msg
end
end
# Forward console input to our channel.
module KeyboardInput
include EM::Protocols::LineText2
def receive_line data
$chat.publish("#{$nick}: #{data}",
:routing_key => $channel)
end
end
EM.open_keyboard(KeyboardInput)
end
But ended up the following error:
chat.rb:11:in `': uninitialized constant AMQP (NameError)
After that, I tried different example code with AMQP at my dev env but all shows me that error. So the problem is not in the code, the problem with my dev env. Can anybody point me out the issues with my dev env. Thanks in advance.
I have AMQP installed and integrated with Ruby (via the bunny gem). Maybe I can help?
Most likely the gem install failed to compile the amqp libs. Uninstall the gem and reinstall, taking a very close look at the messages produced. Possibly you're only missing some third-party libs.
Which platform are you on?
Related
I am trying to run the Kafka Consumer example found here:
http://www.rubydoc.info/gems/ruby-kafka/Kafka/Consumer
This is the program I am trying to run:
require "kafka"
kafka = Kafka.new(["kafka1:9092", "kafka2:9092"])
# Create a new Consumer instance in the group `my-group`:
consumer = kafka.consumer(group_id: "my-group")
# Subscribe to a Kafka topic:
consumer.subscribe("messages")
# Loop forever, reading in messages from all topics that have been
# subscribed to.
consumer.each_message do |message|
puts message.topic
puts message.partition
puts message.key
puts message.value
puts message.offset
end
But whenever I run this program I get the error:
example.rb:3:in '<main>': undefined method 'new' for Kafka:Module (NoMethodError)
I have seen this sort of error of Undefined method 'new' in a few questions relating to completely different programs but can't seem to figure out what is wrong as I haven't overwritten a kafka class or anything. I installed kafka on my machine using sudo gem install kafka-rb the version being kafka-rb-0.0.15.
Does anyone have any ideas why the problem is occurring and how to resolve this issue?
As was mentioned in the comments, you are using kafka-rb gem, which is no longer maintained. But if for some reason you want to continue using it, the code would probably look something like this:
require 'kafka'
consumer = Kafka::Consumer.new(topic: 'messages')
consumer.loop do |messages|
puts "Received"
puts messages
end
Sir, I follow the link https://github.com/eventmachine/eventmachine/wiki/Building-EventMachine-with-SSL-on-Windows
to install eventmachine gem in my windows system.
The gem got successfully installed.
But, I am getting this following error, when I used the following piece of code to connect to websocket and tried to fetch some data.
require 'faye/websocket'
require 'eventmachine'
require 'json'
EM.run {
ws = Faye::WebSocket::Client.new('wss://ws.binaryws.com/websockets/v3')
ws.on :open do |event|
p [:open]
ws.send(JSON.generate({ticks: 'frxUSDJPY'}))
end
ws.on :message do |event|
p [:message, event.data]
end
}
Please help.
terminate called after throwing an instance of 'std::runtime_error'
what(): Encryption not available on this event-machine
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
i'm looking for a way to open and use websockets from within a Padrino application. i know Padrino works with a single thread but i'm looking for a way to open websockets and share variables between its "onopen" "onclose" "onmessage" methods and Padrino controllers.
any idea how it's done ?
links i looked into:
Examples of Eventmachine usage with Padrino and Sinatra (only Sinatra worked for me)
em-websocket on GitHub
UPDATE 1:
this is my main.rb:
require 'rubygems' # <-- Added this require
require 'em-websocket'
require 'padrino-core'
require 'thin'
require File.expand_path("../config/boot.rb", __FILE__)
SOCKETS = []
EventMachine.run do # <-- Changed EM to EventMachine
# class App < Sinatra::Base
# get '/' do
# SOCKETS.each {|s| s.send "fooooo"}
# return "foo"
# end
# end
EventMachine::WebSocket.start(:host => '0.0.0.0', :port => 8080) do |ws| # <-- Added |ws|
# Websocket code here
ws.onopen {
ws.send "connected!!!!"
SOCKETS << ws
}
ws.onmessage { |msg|
puts "got message #{msg}"
ws.send "ECHO: #{msg}"
}
ws.onclose {
ws.send "WebSocket closed"
SOCKETS.delete ws
}
end
# You could also use Rainbows! instead of Thin.
# Any EM based Rack handler should do.
#App.run!({:port => 3000}) # <-- Changed this line from Thin.start to App.run!
Thin::Server.start Padrino.application, '0.0.0.0', 3000
end
i'm getting this exception:
/home/cstore/.rvm/gems/ruby-1.9.2-p290#runtime/gems/thin-1.2.11/lib/thin/daemonizing.rb:2:in `require': no such file to load -- daemons (LoadError)
from /home/cstore/.rvm/gems/ruby-1.9.2-p290#runtime/gems/thin-1.2.11/lib/thin/daemonizing.rb:2:in `<top (required)>'
from /home/cstore/.rvm/gems/ruby-1.9.2-p290#runtime/gems/thin-1.2.11/lib/thin/server.rb:50:in `<class:Server>'
from /home/cstore/.rvm/gems/ruby-1.9.2-p290#runtime/gems/thin-1.2.11/lib/thin/server.rb:48:in `<module:Thin>'
from /home/cstore/.rvm/gems/ruby-1.9.2-p290#runtime/gems/thin-1.2.11/lib/thin/server.rb:1:in `<top (required)>'
from main.rb:39:in `block in <main>'
from /home/cstore/.rvm/gems/ruby-1.9.2-p290#runtime/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `call'
from /home/cstore/.rvm/gems/ruby-1.9.2-p290#runtime/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
from /home/cstore/.rvm/gems/ruby-1.9.2-p290#runtime/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
from main.rb:9:in `<main>'
UPDATE 2:
Resolved thanks to Nathan !
I just added 'daemons' to Gemfile and reloaded my application.
Maybe you need to install daemons:
Edit your Gemfile:
# Adding this
gem 'daemons'
Install missing gems:
$ bundle install
What in particular from this example: https://github.com/igrigorik/em-websocket and Any success with Sinatra working together with EventMachine WebSockets? didn't work with Padrino but did with Sinatra? Can you explain the errors you got and why those examples failed (stacktraces)? Maybe we can help investigate.
I ran across this post and it helped me a bit, but I wanted to offer an alternative solution to anyone else who might stumble upon it. I chose to just directly modify the config.ru and mount a websocket-rack application.
Here's my config.ru where WSApp is a subclass of Rack::WebSocket::Application and is placed in the lib/ directory (therefore being automatically loaded by Padrino):
#!/usr/bin/env rackup
# encoding: utf-8
# This file can be used to start Padrino,
# just execute it from the command line.
require File.expand_path("../config/boot.rb", __FILE__)
# Setup routes
map '/' do
run Padrino.application
end
map '/ws' do
run WSApp.new
end
Since this is the top hit in Google right now, I'd like to link it to padrino-websockets, a clean DSL for writing websockets applications in Padrino.
I have this code in Ruby waitr ..
$LOAD_PATH.unshift File.join(File.dirname(__FILE__),'..') if $0 == __FILE__
require 'test/unit'
require 'Watir'
require 'Watir/contrib/enabled_popup'
class TC_Dialog_Test<Test::Unit::TestCase
$ie=Watir::IE.new
$ie.bring_to_front()
$myDir = File.expand_path(File.dirname(__FILE__))
def setup
$ie.goto "file://#{$myDir}/aa.html"
end
def test_confirm_OK
sleep(3)
$ie.button(:id, 'btnConfirm').click_no_wait
$hwnd = $ie.enable_popup(5)
if ($hwnd)
$popup =WinClicker.new
$popup.makeWindowsActive($hwnd)
sleep(3)
# $popup.clickWindowsButton($hwnd,"OK")
puts "you pressed Comfirm-ok"
sleep(3)
end
end
end
when runnung this code get this error results ...
test_confirm_OK(TC_Dialog_Test):
WIN32OLERuntimeError: unknown property or method `document'
HRESULT error code:0x800706b5
The interface is unknown.
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.5.3/./watir/ie.rb:417:in `method_missing'
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.5.3/./watir/ie.rb:417:in `document'
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.5.3/./watir/container.rb:767:in `ole_inner_elements'
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.5.3/./watir/container.rb:838:in `locate_input_element'
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.5.3/./watir/input_elements.rb:10:in `locate'
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.5.3/./watir/element.rb:47:in `assert_exists'
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.5.3/./watir/element.rb:278:in `enabled?'
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.5.3/./watir/element.rb:53:in `assert_enabled'
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.5.3/./watir/element.rb:214:in `click_no_wait'
Mo.rb:16:in `test_confirm_OK'
You are using Watir 1.5.3, but the current stable is 1.8.1. I suggest you to update your gem to the latest and try again. There is a good chance that the WIN32OLE object for IE has changed it's behavior and old gem is conflicting with it. (Have you updated IE or smth since last successful run?)
I got the same error using Watir 1.8.1. I don't know if there is anything wrong with your code, but in my case the tests ran fine on other machines.
I am running my tests in RubyMine 3.1.x. Running RubyMine as Administrator allowed Ruby to have the rights to interact with IE properly and the problem went away.
Edit:
Found the forum post which originally helped me solve the problem.
This is the code I am trying to run as a service.
require 'rubygems'
require 'win32/daemon'
require 'win32/service'
include Win32
class Daemon
def service_main
while running?
sleep 3
File.open("c:\\test.log", "a"){ |f| f.puts "service is running" }
end
end
def service_stop
exit!
end
end
Daemon.mainloop
This is the code I use to register the Service
require 'rubygems'
require 'win32/service'
include Win32
SERVICE_NAME = 'ruby_sample1'
# Create a new service
ser = Service.create({
:service_name => SERVICE_NAME,
:service_type => Service::WIN32_OWN_PROCESS,
:description => 'A custom service I wrote just for fun',
:start_type => Service::AUTO_START,
:error_control => Service::ERROR_NORMAL,
:binary_path_name => 'c:\\Ruby186\\bin\\ruby.exe -C c:\\temp\\test.rb',
:load_order_group => 'Network',
:dependencies => ['W32Time','Schedule'],
:display_name => SERVICE_NAME
})
After the service is registered I try to start the service from services.msc. I get an error that says "Error 1053: The service did not respond to the start or control request in a timely fashion"
open an irb session and say - require 'win32/daemon'
Most likely you'll get the answer to 1053 problem especially if you have installed win32-service gem for platform mswin32.
I had the same problem and win32-service gem just won't build for platform ruby on my machine even after installing devkit. It persistently gave me following error
win32/daemon.c:141:7: error: '__try' undeclared (first use in this function)
Eventually I ended by building win32-service gem from the latest code on github.