Simple send message

335 views
Skip to first unread message

Tom

unread,
Aug 14, 2009, 10:31:47 AM8/14/09
to Blather
Hi,

thanks for Blather! I am currently working on replacing the leaking
XMPP4R libs with blather and have a hard time doing one simple thing.

In a stand-alone process (using daemon-kit @ github) I would like to
start a connection, fetch messages from my internal message queue
(beanstalkd) and send them out using the established XMPP connection.

Something along these lines:

require 'blather/client'

client = Blather::Client.setup 'spid...@jabber.org',
'34532452ertwer'
client.run
client.register_handler(:ready) { puts "Connected ! send messages." }
jid = Blather::JID.new('supe...@gmail.com')
client.write Blather::Stanza::Message.new(jid, "Hi from down under.")

Can I do this elegantly using the DSL or should I use the client? All
the examples I found rely on having a handler waiting for a certain
message and the simply saying "write or say something". I am waiting
for an event from my queue, not from XMPP, so I obviously need another
approach. Any help / pointers is appreciated.

Thanks, Tom

Jeff Smick

unread,
Aug 14, 2009, 1:01:47 PM8/14/09
to xmpp-b...@googlegroups.com
Hi Tom,

Blather has its own daemonization logic so you don't necessarily need
to use daemon-kit.
Considering Blather runs on EventMachine the "Jack" library
(http://github.com/dj2/jack/tree/master) for beanstalk would probably
be the best fit.
The Blather way of doing what you're asking might then look like this:

require 'rubygems'
require 'blather/client'

def process(job)
say 'supe...@gmail.com', job.body
end

beanstalk = Jack::Connection.new
beanstalk.callback { |job| process job }

setup 'spid...@jabber.org', '34532452ertwer'
when_ready { beanstalk.reserve }


I haven't used beanstalkd for anything so I may be getting something wrong.

--Jeff

Tom

unread,
Aug 15, 2009, 5:37:53 PM8/15/09
to Blather
Hi Jeff,

thanks for this very helpful post! I hope it'll help others as well to
appreciate blather and get it up and running quickly.
Concerning the daemonization logic in Blather: This is what you
demonstrate in the pingpong_one_process example, correct?
Currently I am fighting an eventmachine problem: 'eventmachine not
initialized: evma_connect_to_server (RuntimeError)'
I'll let you know how things go. Thanks again!
I am always amazed in how little lines one can solve a complex task in
Ruby with the right gems.

On Aug 14, 7:01 pm, Jeff Smick <sprsqu...@gmail.com> wrote:
> Hi Tom,
>
> Blather has its own daemonization logic so you don't necessarily need
> to use daemon-kit.
> Considering Blather runs on EventMachine the "Jack" library
> (http://github.com/dj2/jack/tree/master) for beanstalk would probably
> be the best fit.
> The Blather way of doing what you're asking might then look like this:
>
> require 'rubygems'
> require 'blather/client'
>
> def process(job)
>   say 'super...@gmail.com', job.body
> end
>
> beanstalk = Jack::Connection.new
> beanstalk.callback { |job| process job }
>
> setup 'spider...@jabber.org', '34532452ertwer'
> when_ready { beanstalk.reserve }
>
> I haven't used beanstalkd for anything so I may be getting something wrong.
>
> --Jeff
>
>
>
> On Fri, Aug 14, 2009 at 7:31 AM, Tom<qkl...@gmail.com> wrote:
>
> > Hi,
>
> > thanks for Blather! I am currently working on replacing the leaking
> > XMPP4R libs with blather and have a hard time doing one simple thing.
>
> > In a stand-alone process (using daemon-kit @ github) I would like to
> > start a connection, fetch messages from my internal message queue
> > (beanstalkd) and send them out using the established XMPP connection.
>
> > Something along these lines:
>
> > require 'blather/client'
>
> > client = Blather::Client.setup 'spider...@jabber.org',
> > '34532452ertwer'
> > client.run
> > client.register_handler(:ready) { puts "Connected ! send messages." }
> > jid = Blather::JID.new('super...@gmail.com')

Jeff Smick

unread,
Aug 15, 2009, 6:23:22 PM8/15/09
to xmpp-b...@googlegroups.com
Hi Tom,

Glad you're enjoying Blather.

Sounds like you're trying to run Blather outside of its EM.run block.
I didn't realize Jack tries to connect when you instantiate its
connection object.


Try putting that in the when_ready block:

when_ready do
beanstalk = Jack::Connection.new
beanstalk.callback { |job| process job }
beanstalk.reserve
end

That'll create a connection to the beanstalk server after EventMachine
has been started and the XMPP connection has been created.

Cheers,
--Jeff

Tom

unread,
Aug 16, 2009, 4:46:15 PM8/16/09
to Blather
Hi Jeff,

this moved things further, but still I can't send messages correctly.
I tried with Jack for a while, but until I found out that I can't even
run the supplied examples without any advanced stuff I retreated to
the simpler beanstalk-client. So I ended up with something like this:

require 'rubygems'
require 'blather/client'
require 'beanstalk-client'

setup 'acc...@gmail.com', 'password

DEFAULT_PORT = 11300
SERVER_IP = '127.0.0.1'
DEFAULT_PRIORITY = 1
TTR = 3

def get_queue(queue_name)
qonnection = Beanstalk::Pool.new(["#{SERVER_IP}:#{DEFAULT_PORT}"])
qonnection.watch(queue_name)
qonnection.use(queue_name)
qonnection.ignore('default')
puts 'Got connection to beanstalk queue: ' + queue_name.to_s
qonnection
end

outQ = get_queue(:outQ)

# Called after the connection has been connected.
handle :ready do
loop do # <--- killer loop!
job = outQ.reserve
puts job.id
say job.ybody[:handle], job.ybody[:body]
job.delete
end
end

I can run this stuff, and it reserves and sends one stanza to my
receiving account (without the 'killer loop'). But, that's it, no
repeating the sending for more messages in the queue. Once I
integrated the crude killer loop, I get all the messaged correctly
reserved, consumed and deleted (and putted to the tty) but blather
doesn't even go online. This probably has something to do with you
thread magic in blather, I presume. Any ideas?

Thanks, Tom

P.S. I contacted DJ2 (the auther of Jack), we'll see what he says.

Jeff Smick

unread,
Aug 17, 2009, 3:53:19 AM8/17/09
to xmpp-b...@googlegroups.com
Hey Tom,

Too bad about Jack, if you manage to get it running it'd fit right in
to what's happening with Blather.

EventMachine doesn't use threads actually, so neither does Blather.
Meaning, anytime you block in your code, you'll block the reactor and
nothing will get processed. EM will allow you to "spawn" a new process
and you could run your loop from there though. It looks like these are
starting to be more about EventMachine related questions than Blather
related.

If you haven't already I highly suggest digging into EventMachine:
http://eventmachine.rubyforge.org. If you have deeper questions about
EM, the guys in #eventmachine on irc.freenode.net are extremely kind
and helpful.

--Jeff

Tom

unread,
Aug 17, 2009, 2:33:03 PM8/17/09
to Blather
Hi Jeff,

thanks for the pointer. I'll join #eventmachine and see what I can
learn. In the meantime I got a kind reply from Dan Sinclair, the man
behind Jack, he as well said that the EM reactor is missing in the
examples, so just inserting EM.run { jack = Jack::Connection.new etc.
should do the trick. All the pointers direct me to EM, so there I go.

Thanks for you help,

Tom

P.S. I will post the working code here in case somebody is interested

On Aug 17, 9:53 am, Jeff Smick <sprsqu...@gmail.com> wrote:
> Hey Tom,
>
> Too bad about Jack, if you manage to get it running it'd fit right in
> to what's happening with Blather.
>
> EventMachine doesn't use threads actually, so neither does Blather.
> Meaning, anytime you block in your code, you'll block the reactor and
> nothing will get processed. EM will allow you to "spawn" a new process
> and you could run your loop from there though. It looks like these are
> starting to be more about EventMachine related questions than Blather
> related.
>
> If you haven't already I highly suggest digging into EventMachine:http://eventmachine.rubyforge.org. If you have deeper questions about
> EM, the guys in #eventmachine on irc.freenode.net are extremely kind
> and helpful.
>
> --Jeff
>
>
>
> On Sun, Aug 16, 2009 at 1:46 PM, Tom<qkl...@gmail.com> wrote:
>
> > Hi Jeff,
>
> > this moved things further, but still I can't send messages correctly.
> > I tried with Jack for a while, but until I found out that I can't even
> > run the supplied examples without any advanced stuff I retreated to
> > the simpler beanstalk-client. So I ended up with something like this:
>
> > require 'rubygems'
> > require 'blather/client'
> > require 'beanstalk-client'
>
> > setup 'acco...@gmail.com', 'password

Tom

unread,
Aug 24, 2009, 6:15:25 AM8/24/09
to Blather
As promised, by now things are humming along nicely. This is how I got
my worker online:

require 'rubygems'
require 'blather/client'
require 'jack'
require 'yaml'

setup 'supe...@gmail.com/outgoingstuff', '987jhfgifztUDZTRdt'

when_ready do
jack = Jack::Connection.new
r = jack.watch('outQ')
r.callback{
process_job = proc{ |job|
message = YAML::load(job.body)
say message[:handle], message[:body]
DaemonKit.logger.info("Sent out #{message[:body]} to #{message
[:handle]} via XMPP.")
r2 = jack.delete(job)
r2.callback {DaemonKit.logger.info("Successfully removed job #
{job.jobid} from queue.")}
d = jack.reserve
d.callback{|j| process_job.call(j)}
}
r = jack.reserve
r.callback{|j| process_job.call(j)}
}
end

As you can see I am working with Daemon-Kit, which doesn't seem to be
necessary. But for now it does the trick. Maybe this helps someone out
there.

Jeff Smick

unread,
Aug 24, 2009, 10:23:19 AM8/24/09
to xmpp-b...@googlegroups.com
Very cool. Thanks Tom!

BTW, is that the real password for your account? Might be time to change it ;o)

--Jeff
Reply all
Reply to author
Forward
0 new messages