Table of Contents


cod comes with a number of built-in special channels. For example, it can talk directly to beanstalkd.

Look at this as an example of how easily cod can be extended to speak protocol X. And also, use these because they make cod useful!

Beanstalkd Channels

Beanstalkd channels work like other cod channels at their base:

  # Connects to beanstalkd on localhost:11300 
  # per default. 
  beanstalk = Cod.beanstalk("my_tube")
  beanstalk.put :foo
  beanstalk.get # => :foo

The symbol :foo is transformed into a message (using serialisation) and put into the tube named ‘my_tube’. It then waits for someone to come a long and ask beanstalkd for a message. Which happens in #get.

Here’s a more extended example:

  beanstalk.try_get do |msg, ctl|
    case msg
      when 1
      when 2
      when 3
        # Exit block without call to ctl - normal
        # reserve/delete cycle that also happens 
        # in #get
      # Raising an exception will release the 
      # message for someone else to fetch.
      fail "Unknown message!"

Please also have a look at Cod::Beanstalk::Channel::Control (ctl above)- almost all commands beanstalkd allows on messages are allowed on this object.

Spawning processes

A lot of server implementations have an inetd mode where you launch the server binary and communicate via stdin/stdout with it. cod captures that pattern in Cod.process:

  proc = Cod.process('uname',
  uname =
  uname.get # => "Darwin"
  # Wait for the process to terminate

Note that we use the line serializer here. This makes cod expect and speak the unix convention where each element of output is terminated by a “\n”.

Note that you can also implement such ‘stdin/stdout’ servers using cod – but only if you are careful not to use these streams for other things:

  stdio = Cod.stdio(
  stdio.put 'test'

This will output


followed by a newline.

Other things to look at

Be sure to also check out Cod.bidir_pipe and the examples in the source tree (examples/) that illustrate some of these concepts.

Cod is the clay in your hands. Go build something.