{"id":15537590,"url":"https://github.com/nullvoxpopuli/action_cable_client","last_synced_at":"2025-04-12T18:54:13.534Z","repository":{"id":56842024,"uuid":"55780013","full_name":"NullVoxPopuli/action_cable_client","owner":"NullVoxPopuli","description":"A ruby client for interacting with Rails' ActionCable. -- Maintainers Wanted.","archived":false,"fork":false,"pushed_at":"2023-12-15T02:53:31.000Z","size":98,"stargazers_count":256,"open_issues_count":6,"forks_count":28,"subscribers_count":11,"default_branch":"master","last_synced_at":"2024-10-29T22:32:36.672Z","etag":null,"topics":["action-cable","action-cable-protocol","em-websocket","hacktoberfest","rails","ruby"],"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/NullVoxPopuli.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-04-08T13:12:27.000Z","updated_at":"2024-10-05T22:40:13.000Z","dependencies_parsed_at":"2023-12-15T03:45:31.743Z","dependency_job_id":"6d9ddb24-03ca-42c8-b48d-23d15a78b2d1","html_url":"https://github.com/NullVoxPopuli/action_cable_client","commit_stats":{"total_commits":74,"total_committers":8,"mean_commits":9.25,"dds":0.1216216216216216,"last_synced_commit":"942886fd79cfa283458cf46ac1d54c2c35ad0cad"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NullVoxPopuli%2Faction_cable_client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NullVoxPopuli%2Faction_cable_client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NullVoxPopuli%2Faction_cable_client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NullVoxPopuli%2Faction_cable_client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NullVoxPopuli","download_url":"https://codeload.github.com/NullVoxPopuli/action_cable_client/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248618262,"owners_count":21134200,"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":["action-cable","action-cable-protocol","em-websocket","hacktoberfest","rails","ruby"],"created_at":"2024-10-02T11:58:19.541Z","updated_at":"2025-04-12T18:54:13.507Z","avatar_url":"https://github.com/NullVoxPopuli.png","language":"Ruby","readme":"# Action Cable Client\n[![Gem Version](https://badge.fury.io/rb/action_cable_client.svg)](https://badge.fury.io/rb/action_cable_client)\n[![Build Status](https://travis-ci.org/NullVoxPopuli/action_cable_client.svg?branch=master)](https://travis-ci.org/NullVoxPopuli/action_cable_client)\n[![Code Climate](https://codeclimate.com/github/NullVoxPopuli/action_cable_client/badges/gpa.svg)](https://codeclimate.com/github/NullVoxPopuli/action_cable_client)\n[![Test Coverage](https://codeclimate.com/github/NullVoxPopuli/action_cable_client/badges/coverage.svg)](https://codeclimate.com/github/NullVoxPopuli/action_cable_client/coverage)\n\nThis gem is a wrapper around [websocket-eventmachine-client](https://github.com/imanel/websocket-eventmachine-client), and supports the Rails Action Cable protocol.\n\n## Usage\n\n```ruby\nrequire 'action_cable_client'\n\nEventMachine.run do\n\n  uri = \"ws://localhost:3000/cable/\"\n  client = ActionCableClient.new(uri, 'RoomChannel')\n  # called whenever a welcome message is received from the server\n  client.connected { puts 'successfully connected.' }\n\n  # called whenever a message is received from the server\n  client.received do | message |\n    puts message\n  end\n\n  # Sends a message to the sever, with the 'action', 'speak'\n  client.perform('speak', { message: 'hello from amc' })\nend\n```\n\nThis example is compatible with [this version of a small Rails app with Action Cable](https://github.com/NullVoxPopuli/mesh-relay/tree/2ed88928d91d82b88b7878fcb97e3bd81977cfe8)\n\n\n\nThe available hooks to tie in to are:\n - `disconnected {}`\n - `connected {}`\n - `subscribed {}`\n - `rejected {}`\n - `errored { |msg| }`\n - `received { |msg }`\n - `pinged { |msg| }`\n\n\n#### Connecting on initialization is also configurable.\n\n```ruby\nclient = ActionCableClient.new(uri, 'RoomChannel', false)\nclient.connect!(headers = {})\n```\n\n```ruby\nclient.pinged do |_data|\n  # you could track the time since you last received a ping, if you haven't\n  # received one in a while, it could be that your client is disconnected.\nend\n```\n\n\nTo reconnect,\n\n```ruby\nclient.reconnect!\n```\n\n#### Sending additional params\n\n```ruby\nparams = { channel: 'RoomChannel', favorite_color: 'blue' }\nclient = ActionCableClient.new(uri, params)\n```\n\nthen on the server end, in your Channel, `params` will give you:\n```\n{\n       \"channel\" =\u003e \"RoomChannel\",\n\"favorite_color\" =\u003e \"blue\"\n}\n```\n\n#### Using Headers\n\n\n```ruby\nparams = { channel: 'RoomChannel', favorite_color: 'blue' }\nclient = ActionCableClient.new(uri, params, true, {\n  'Authorization' =\u003e 'Bearer token'\n})\n```\n\n#### Using TLS\n\nExample given for client certificate authentication. See EventMachine::Connection#start_tls documentation for other options. \n\n```ruby\nparams = { channel: 'RoomChannel', favorite_color: 'blue' }\ntls = {cert_chain_file: 'user.crt', private_key_file: 'user.key'}\nclient = ActionCableClient.new(uri, params, true, nil, tls)\n```\n\n\n## Demo\n\n[![Live Demo](http://img.youtube.com/vi/x9D1wWsVHMY/mqdefault.jpg)](http://www.youtube.com/watch?v=x9D1wWsVHMY\u0026hd=1)\n\nAction Cable Client Demo on YouTube (1:41)\n\n[Here is a set of files in a gist](https://gist.github.com/NullVoxPopuli/edfcbbe91a7877e445cbde84c7f05b37) that demonstrate how different `action_cable_client`s can communicate with eachother.\n\n## The Action Cable Protocol\n\nThere really isn't that much to this gem. :-)\n\n1. Connect to the Action Cable URL\n2. After the connection succeeds, send a subscribe message\n  - The subscribe message JSON should look like this\n    - `{\"command\":\"subscribe\",\"identifier\":\"{\\\"channel\\\":\\\"MeshRelayChannel\\\"}\"}`\n  - You should receive a message like this:\n    - `{\"identifier\"=\u003e\"{\\\"channel\\\":\\\"MeshRelayChannel\\\"}\", \"type\"=\u003e\"confirm_subscription\"}`\n3. Once subscribed, you can send messages.\n  - Make sure that the `action` string matches the data-handling method name on your ActionCable server.\n  - Your message JSON should look like this:\n    - `{\"command\":\"message\",\"identifier\":\"{\\\"channel\\\":\\\"MeshRelayChannel\\\"}\",\"data\":\"{\\\"to\\\":\\\"user1\\\",\\\"message\\\":\\\"hello from user2\\\",\\\"action\\\":\\\"chat\\\"}\"}`\n    - Received messages should look about the same\n\n4. Notes:\n  - Every message sent to the server has a `command` and `identifier` key.\n  - The channel value must match the `name` of the channel class on the ActionCable server.\n  - `identifier` and `data` are redundantly jsonified. So, for example (in ruby):\n```ruby\npayload = {\n  command: 'command text',\n  identifier: { channel: 'MeshRelayChannel' }.to_json,\n  data: { to: 'user', message: 'hi', action: 'chat' }.to_json\n}.to_json\n```\n\n\n## Contributing\n\n1. Fork it ( https://github.com/NullVoxPopuli/action_cable_client/fork )\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnullvoxpopuli%2Faction_cable_client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnullvoxpopuli%2Faction_cable_client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnullvoxpopuli%2Faction_cable_client/lists"}