{"id":17680274,"url":"https://github.com/kostya/em-nodes","last_synced_at":"2025-03-30T18:48:41.500Z","repository":{"id":16223245,"uuid":"18970549","full_name":"kostya/em-nodes","owner":"kostya","description":"Simple abstraction on top of EventMachine for easy create clients, servers, workers, ...","archived":false,"fork":false,"pushed_at":"2015-06-16T16:33:52.000Z","size":404,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-06T07:51:10.434Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kostya.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-04-20T18:13:33.000Z","updated_at":"2015-06-05T23:03:35.000Z","dependencies_parsed_at":"2022-07-21T06:47:23.643Z","dependency_job_id":null,"html_url":"https://github.com/kostya/em-nodes","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kostya%2Fem-nodes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kostya%2Fem-nodes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kostya%2Fem-nodes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kostya%2Fem-nodes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kostya","download_url":"https://codeload.github.com/kostya/em-nodes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246365640,"owners_count":20765546,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-24T09:06:24.492Z","updated_at":"2025-03-30T18:48:40.650Z","avatar_url":"https://github.com/kostya.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EM::Nodes\n\nSimple abstraction on top of EventMachine for easy create clients, servers, workers, ...\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'em-nodes'\n\n### How to use\n\nClient should inherit from EM::Nodes::Client, Server should inherit from EM::Nodes::Server.\nClient and Server can define and use methods `on_#{method_name}` and `send_#{method_name}`, `on_#{method_name}` - receive value from `send_#{method_name}` call.\n\nExample: client after connection to server, send i_connected to server, server send_client_request, client on_client_request send to server client_responce, and server output value.\n\nsimple_server.rb\n```ruby\nclass Server \u003c EM::Nodes::Server\n  def on_i_connected(value)\n    puts \"#{value} connected\"\n    send_client_request(1)\n  end\n\n  def on_client_responce(value)\n    puts \"client responce #{value}\"\n  end\nend\n\nEM.run do\n  Server.start \"/tmp/server.sock\"\nend\n```\n\nsimple_client.rb\n```ruby\nclass Client \u003c EM::Nodes::Client\n  def on_client_request(value)\n    send_client_responce(value + 1)\n  end\nend\n\nEM.run do\n  client = Client.connect \"/tmp/server.sock\"\n  client.send_i_connected(\"hahaha\")\nend\n```\n\n### HelloFeature\n\nHelloFeature used to simple greetings between client and server. Client should define method `info` which return hash. Server had added method: `ready_clients`, which contain clients which answered on `info` method.\n\nhello_server.rb\n```ruby\nclass Server \u003c EM::Nodes::Server\n  include HelloFeature\nend\n\nEM.run do\n  Server.start \"/tmp/server.sock\"\n\n  EM.add_periodic_timer(5) do\n    vasya = Server.ready_clients.detect { |client| client.data.my_name == 'Vasya' }\n    vasya.send_die if vasya\n  end\nend\n```\n\nhello_client.rb\n```ruby\nclass Client \u003c EM::Nodes::Client\n  include HelloFeature\n\n  def initialize(name)\n    @name = name\n    super\n  end\n\n  def info\n    { :my_name =\u003e @name }\n  end\n\n  def on_die\n    puts \"oops i should die\"\n    EM.stop\n  end\nend\n\nEM.run do\n  client = Client.connect \"/tmp/server.sock\", nil, \"Vasya\"\nend\n```\n\n### TaskFeature\n\nTaskFeature is simple task sending, executing and getting results. Client should define method `on_task(task_id, param)`, and when execute task and got results, should call `send_task_result(task_id, result)`. Server should send task with method, `send_task(param)`\n\nExample: Client after connection send to server `get_me_task`, server send_task to client with rand value, client execute `on_task` method and add to param 1 and call `send_task_result`, server receive `on_task_result` and output result.\n\ntask_server.rb\n```ruby\nclass Server \u003c EM::Nodes::Server\n  include TaskFeature\n\n  def on_get_me_task\n    param = rand\n    puts \"send to client #{param}\"\n    send_task(param)\n  end\n\n  def on_task_result(result)\n    puts \"client return result #{result}\"\n  end\nend\n\nEM.run do\n  Server.start \"/tmp/server.sock\"\nend\n```\n\ntask_client.rb\n```ruby\nclass Client \u003c EM::Nodes::Client\n  include TaskFeature\n\n  def on_task(task_id, param)\n    EM.next_tick { send_task_result(task_id, param + 1) }\n  end\nend\n\nEM.run do\n  client = Client.connect \"/tmp/server.sock\"\n  client.send_get_me_task\nend\n```\n\n### DefaultClient, DefaultServer\n\nThis is abstraction with HelloFeature and TaskFeature by default. Example how to create server and 10 workers. Server schedule for each client every 1 second, some value. Client just add 1 to value and return result to server, server puts value.\n\ndefault_server.rb\n```ruby\nclass Server \u003c EM::Nodes::DefaultServer\n  def on_task_result(res)\n    puts res\n  end\nend\n\nEM.run do\n  Server.start \"127.0.0.1\", 8888\n\n  # periodical schedule tasks to clients\n  EM.add_periodic_timer(1) do\n    Server.ready_clients.each do |client|\n      client.send_task(rand)\n    end\n  end\nend\n```\n\ndefault_client.rb\n```ruby\nclass Client \u003c EM::Nodes::DefaultClient\n  def info\n    { :name =\u003e 'bla' }\n  end\n\n  def on_task(task_id, param)\n    res = do_something(param)\n    send_task_result(task_id, res)\n  end\n\n  def do_something(x)\n    x + 1\n  end\nend\n\nEM.run do\n  10.times do\n    Client.connect \"127.0.0.1\", 8888\n  end\nend\n```\n\n[More examples](https://github.com/kostya/em-nodes/tree/master/examples)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkostya%2Fem-nodes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkostya%2Fem-nodes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkostya%2Fem-nodes/lists"}