@algonauti/ember-cable

中文文档
2.0.0发布于 2026/10/16MITTypeScriptCommonJS近 30 天下载 2462

Power your ember.js application with actioncable

原始使用文档
文档目录5 节
  1. Installation
  2. Basic Usage
  3. TypeScript
  4. Contributing
  5. License

ember-cable

This add-on enables simple integration of Rails Action Cable into Ember apps.

CI Ember Observer Score

v2.0.0 is published as a v2 addon (Embroider format). It requires ember-auto-import >= 2 and Ember >= 4.12. Repository layout: ember-cable/ (the addon) and test-app/ (tests).

Installation

run the following command from inside your ember-cli project:

ember install @algonauti/ember-cable

Basic Usage

Once the addon is installed, the cable service can be injected wherever needed in the application.

// test-app/app/components/notification-messages/component.js
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import { debug, inspect } from '@ember/debug';
import EmberObject from '@ember/object';
import { getOwner } from '@ember/application';

export default class NotificationMessagesComponent extends Component {
  @service cable;
  @service notification;

  constructor() {
    super(...arguments);
    this._setupConsumer();
  }

  _setupConsumer() {
    let consumer = this.cable.createConsumer('ws://localhost:4200/cable');

    consumer.createSubscription('BroadcastChannel', {
      connected() {
        debug('BroadcastChannel#connected');
        this.perform('ping');
      },
      received(data) {
        debug( "received(data) -> " + inspect(data) );
      },
      disconnected() {
        debug("BroadcastChannel#disconnected");
      }
    });

    // Passing Parameters to Channel
    let subscription = consumer.createSubscription({ channel: 'BroadcastChannel', room: 'BestRoom' }, {
      connected() {
        this.perform('ping', { foo: 'bar' });
      },
      received: (data) => {
        this._updateRecord(data);
      },
      disconnected: () => {
        this.notification.notify("BroadcastChannel#disconnected");
      }
    });

    // Using mixin and inject your services:
    let subscriptionHandler = EmberObject.extend({
      notification2: service('notification'),

      connected() {
        this.notification2.notify("subscriptionHandler#connected");
      },
    }).create(getOwner(this).ownerInjection());

    consumer.createSubscription({ channel: 'BroadcastChannel' }, subscriptionHandler);

    setTimeout(() => {
      subscription.perform('ping', { foo: 'bar' });
    }, 3000);

    setTimeout(() => {
      this.cable.destroy();
    }, 9000);

  }

  _updateRecord(data) {
     debug( "updateRecord(data) -> " + inspect(data) );
   }
}

TypeScript

The addon is written in TypeScript and ships its own type declarations.

import Component from '@glimmer/component';
import { service } from '@ember/service';
import type CableService from '@algonauti/ember-cable/services/cable';

export default class Notifications extends Component {
  @service declare cable: CableService;

  constructor(owner: unknown, args: object) {
    super(owner, args);

    const consumer = this.cable.createConsumer('ws://localhost:4200/cable');

    consumer.createSubscription('BroadcastChannel', {
      connected() {
        this.perform('ping');
      },
      received(data: unknown) {
        console.log(data);
      },
    });
  }
}

Consumer, CableService and the ActionCable types (BaseMixin, ChannelNameWithParams, Subscription, Subscriptions) are also exported from @algonauti/ember-cable.

Contributing

See the Contributing guide for details.

License

ember-cable is released under the MIT License.

版本与发布记录

@algonauti/ember-cable 共发布 9 个版本,首次发布于 2019/07/15;过去 12 个月发布了 1 个版本。

版本发布日期距今
2.0.0latest2026/10/1622 天前
1.4.02022/06/234 年前
1.3.02021/12/024 年前
1.2.02021/04/125 年前
1.1.02021/01/115 年前
1.0.02020/05/056 年前

关于 @algonauti/ember-cable 的常见问题

@algonauti/ember-cable 是什么?

Power your ember.js application with actioncable

如何安装 @algonauti/ember-cable?

在项目目录运行 npm install @algonauti/ember-cable,或使用 pnpm add / yarn add / bun add。

@algonauti/ember-cable 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@algonauti/ember-cable 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@algonauti/ember-cable 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 20.19。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@algonauti/ember-cable 可以商用吗?

@algonauti/ember-cable 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@algonauti/ember-cable 的下载量有多少?

根据 npm 官方下载统计,@algonauti/ember-cable 在 2026/10/06 – 2026/11/04 这 30 天内被下载 2,462 次,日均约 82 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@algonauti/ember-cable 还在维护吗?

最新版本 2.0.0 发布于 2026/10/16(22 天前);至今共发布 9 个版本,其中过去 12 个月发布了 1 个;首次发布于 2019/07/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。