------art_83052_22007256.1158670339464
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On 9/19/06, hemant <gethemant / gmail.com> wrote:
>
>  Basically what i want is...push the data to the clients
> asynchronously. Using recieve_data I can change @local_symbols and
> hence the data pushed to the client should change accordingly.
>
> So, How do i do this?


The answer depends on when your asynch data becomes available. If it happens
on a time basis, then #add_periodic_timer  is the right solution.
#add_periodic_timer (or #add_timer) is client-specific because you pass it a
block, which is of course a closure, so it has access to your local scope.
The following code will do what you want. Try it with multiple connections
and see:

def receive_data data
  local_scope_data  whatever\n"
  EventMachine.add_periodic_timer(2) {
    send_data( local_scope_data )
  }
end


If your async data comes from other events in the system (like calls to
web-services) then you can use EventMachine's Deferrable pattern, which
works much like the one in Twisted, but easier to use because it's Ruby :-).
If you want to do that, feel free to write me offlist and I'll show you how
to use it.

Unrelated comment: I'm finding that a lot of people have a need to accept
network connections, receive data from remote clients, and then respond with
data that is aggregated from other network services (databases, other web
sites, etc). This is exactly the kind of pattern EventMachine is good for.
I'm thinking about adding some specific API support tosimplify this pattern,
especially for use within Rails apps (where your controller may need to get
data from one or more outside sources to send back to your client). Is this
of interest to anyone?

------art_83052_22007256.1158670339464--