What is the order of functions called?

These will only be called when a connection is made (though a connection which is attempted will also call a few before it fails).

With a client

  1. EM.connect( '127.0.0.1', port, EchoClient?) called first
  2. initialize method (called by EM on a new instance—currently without any parameters and only internally)
  3. post_init (always called)
  4. block given to EM.connect (always called)
  5. connection_completed (called on success, else unbind)
  6. receive_data (called by EM—when it receives incoming data and it’s that port’s turn to process it—it will pass it “all or most” of incoming data).
  7. send_data, send_file_data, send_datagram called by you—puts those things in EM’s outbound queue for that connection.
  8. close_connection, close_connection_after_writing, EM::(stop | stop_event_loop) all shut it down (called by you).
  9. unbind

With a server (only on incoming connections, once per connection):

  1. initialize
  2. post_init (called by EM)
  3. EM.start_server block (the block you passed to the start server call, called by EM)
  4. send_data/send_file_data/send_datagram (called by you)
  5. receive_data (called by EM)
  6. stop_server (called by you — stops server from listening, though existing connections are still live) — see documentation
  7. close_connection, close_connection_after_writing, EM::(stop | stop_event_loop) all shut it down (called by you).
  8. unbind (when socket closed) (called by EM, whenever it closes because of you or the connecting peer)

Which are the methods you can redefine, then, that gets called?

post_init, receive_data, unbind, and, for clients, connection_completed.

What if I have a server connect to a client on the same host running within the same EM—which one gets executed first? From experimentation, it appears to be post_init(client), client block, post_init server, server block, connection_completed(client), then unbind of the one that makes the first call to close_connection (i.e. if server calls close_connection, its unbind is called first [right then?], then the clients’).