{"id":15356362,"url":"https://github.com/saravanabalagi/action-cable-react-jwt","last_synced_at":"2025-10-19T01:48:11.272Z","repository":{"id":57172767,"uuid":"83600864","full_name":"saravanabalagi/action-cable-react-jwt","owner":"saravanabalagi","description":"Rails action-cable integration with JWT authentication","archived":false,"fork":false,"pushed_at":"2023-07-05T03:03:23.000Z","size":22,"stargazers_count":38,"open_issues_count":5,"forks_count":17,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-02-23T07:07:00.141Z","etag":null,"topics":["actioncable","jwt","jwtauth","react","react-native","websocket-client"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/saravanabalagi.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2017-03-01T20:56:50.000Z","updated_at":"2021-10-22T01:31:40.000Z","dependencies_parsed_at":"2024-06-18T19:45:41.783Z","dependency_job_id":"49ce0559-cf80-42ac-a811-fabf17680404","html_url":"https://github.com/saravanabalagi/action-cable-react-jwt","commit_stats":null,"previous_names":["zekedran/action-cable-react-jwt"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saravanabalagi%2Faction-cable-react-jwt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saravanabalagi%2Faction-cable-react-jwt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saravanabalagi%2Faction-cable-react-jwt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saravanabalagi%2Faction-cable-react-jwt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/saravanabalagi","download_url":"https://codeload.github.com/saravanabalagi/action-cable-react-jwt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240972633,"owners_count":19886998,"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":["actioncable","jwt","jwtauth","react","react-native","websocket-client"],"created_at":"2024-10-01T12:28:29.949Z","updated_at":"2025-10-04T00:56:59.981Z","avatar_url":"https://github.com/saravanabalagi.png","language":"JavaScript","readme":"# action-cable-react-jwt\n\nSame as [action-cable-react](https://github.com/schneidmaster/action-cable-react), but allows authenticating websockets using JWTs\n\n## Installation\n\nYarn:\n\n```javascript\nyarn add action-cable-react-jwt\n\n```\n\nnpm:\n\n```javascript\nnpm install action-cable-react-jwt\n\n```\n\n\n## Usage\n\nImport action-cable-react-jwt\n\n```javascript\nimport ActionCable from 'action-cable-react-jwt.js';\n\n// if you don't use ES6 then use\n// const ActionCable = require('action-cable-react-jwt.js');\n\n```\n\nCreating an actioncable websocket\n\n```javascript\nlet App = {};\nApp.cable = ActionCable.createConsumer(\"ws://cable.example.com\", jwt) // place your jwt here\n\n// you shall also use this.cable = ActionCable.createConsumer(...)\n// to create the connection as soon as the view loads, place this in componentDidMount\n```\n\nSubscribing to a Channel for Receiving data\n\n```javascript\nthis.subscription = App.cable.subscriptions.create({channel: \"YourChannel\"}, {\n      connected: function() { console.log(\"cable: connected\") },             // onConnect\n      disconnected: function() { console.log(\"cable: disconnected\") },       // onDisconnect\n      received: (data) =\u003e { console.log(\"cable received: \", data); }         // OnReceive\n}\n\n```\n\nSend data to a channel\n\n```javascript\nthis.subscription.send('hello world')\n\n```\n\nCall a method on channel with arguments\n\n```javascript\nthis.subscription.perform('method_name', arguments)\n\n```\n\nIn your `ApplicationCable::Connection` class in Ruby add\n\n```ruby\n# app/channels/application_cable/connection.rb\nmodule ApplicationCable\n  class Connection \u003c ActionCable::Connection::Base\n    identified_by :current_user\n\n    def connect\n      self.current_user = find_verified_user\n    end\n\n    private\n\n    def find_verified_user\n      begin\n        header_array = request.headers[:HTTP_SEC_WEBSOCKET_PROTOCOL].split(',')\n        token = header_array[header_array.length-1]\n        decoded_token = JWT.decode token.strip, Rails.application.secrets.secret_key_base, true, { :algorithm =\u003e 'HS256' }\n        if (current_user = User.find((decoded_token[0])['sub']))\n          current_user\n        else\n          reject_unauthorized_connection\n        end\n      rescue\n        reject_unauthorized_connection\n      end\n    end\n\n  end\nend\n```\n\nAnd in YourChannel.rb\n\n```ruby\n# app/channels/you_channel.rb\nclass LocationChannel \u003c ApplicationCable::Channel\n\n  # calls connect in client\n  def subscribed\n    stream_from 'location_user_' + current_user.id.to_s\n  end\n\n  # calls disconnect in client\n  def unsubscribed\n    # Any cleanup needed when channel is unsubscribed\n  end\n  \n  # called when send is called in client\n  def receive(params)\n    print params[:data]\n  end\n  \n  # called when perform is called in client\n  def method_name(params)\n    print params[:data]\n  end\n  \nend\n```\n\nRemove a subscription from cable\n\n```javascript\nApp.cable.subscriptions.remove(this.subscription)\n\n// Place this in componentWillUnmount to remove subscription on exiting app\n\n```\n\nAdd a subscription to cable\n\n```javascript\nApp.cable.subscriptions.add(this.subscription)\n\n```\n\nQuerying url and jwt from cable\n\n```javascript\nconsole.log(App.cable.jwt);\nconsole.log(App.cable.url);\n\n```\n\nQuerying subscriptions and connection from cable\n\n```javascript\nconsole.log(App.cable.subscriptions);\nconsole.log(App.cable.connection);\n\n```\n\n\n\n## License\n\nMIT\n\nCopyright (c) 2017 Zeke\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaravanabalagi%2Faction-cable-react-jwt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsaravanabalagi%2Faction-cable-react-jwt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaravanabalagi%2Faction-cable-react-jwt/lists"}