# Event Machine - callbacks from different connections

**URL:** <https://www.ruby-forum.com/t/event-machine-callbacks-from-different-connections/130791>\
**Category:** Ruby\
**Created:** [March 3, 2008, 12:11pm UTC](https://www.ruby-forum.com/t/event-machine-callbacks-from-different-connections/130791 "2008-03-03T12:11:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [March 3, 2008, 12:11pm UTC](https://www.ruby-forum.com/t/event-machine-callbacks-from-different-connections/130791/1 "2008-03-03T12:11:28Z")

</div>

In the RDoc for EventMachine, the following statement is made:  
“You can also call send\_data to write to a connection other than the one  
whose callback you are calling send\_data from. This is done by recording  
the value of the connection in any callback function (the value self),  
in any variable visible to other callback invocations on the same or  
different connection objects. (Need an example to make that clear.)”

If I have the following code, how can I make it so that the ‘send\_data’  
method for ServerOne is called when data is received for ServerTwo?  
i.e. I want a packet of data to be sent out on port 1700 when a packet  
is received on port 1800 (but i need the listener on port 1700 to stay  
listening)

require ‘rubygems’  
require ‘eventmachine’

module ServerOne  
def receive\_data data  
sender\_info = get\_peername[2,6].unpack “nC4”  
port = sender\_info[0]  
ip = sender\_info[1…4].join(".").to\_s  
puts “IP = #{ip}”  
puts “Port = #{port}”  
end  
end

module ServerTwo  
def receive\_data data  
sender\_info = get\_peername[2,6].unpack “nC4”  
port = sender\_info[0]  
ip = sender\_info[1…4].join(".").to\_s  
puts “IP = #{ip}”  
puts “Port = #{port}”  
end  
end

EventMachine::run {  
EventMachine::open\_datagram\_socket “192.168.0.2”, 1700, ServerOne  
EventMachine::open\_datagram\_socket “192.168.0.2”, 1800, ServerTwo  
}

Many thanks

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [March 3, 2008, 1:32pm UTC](https://www.ruby-forum.com/t/event-machine-callbacks-from-different-connections/130791/2 "2008-03-03T13:32:51Z")

</div>

The idea is that there’s a collection of the connection objects held  
outside of the servers and used where necessary. In your case,  
something like this would work:

$one\_conns = []  
$two\_conns = []

module ServerOne  
def receive\_data data  
sender\_info = get\_peername[2,6].unpack “nC4”  
port = sender\_info[0]  
ip = sender\_info[1…4].join(".").to\_s  
puts “IP = #{ip}”  
puts “Port = #{port}”

```
# find your $two_conn and

```

$two\_conns[i].send\_data …  
end  
end

module ServerTwo  
def receive\_data data  
sender\_info = get\_peername[2,6].unpack “nC4”  
port = sender\_info[0]  
ip = sender\_info[1…4].join(".").to\_s  
puts “IP = #{ip}”  
puts “Port = #{port}”

```
 #Find your one conn and
 $one_conns[i].send_data ...

```

end  
end

EventMachine::run {  
EventMachine::open\_datagram\_socket “192.168.0.2”, 1700, ServerOne do  
|conn|  
$one\_conns \<\< conn  
end

EventMachine::open\_datagram\_socket “192.168.0.2”, 1800, ServerTwo do  
|conn|  
$two\_conns \<\< conn  
end  
}

Jason
