@feathersjs/socket-commons

中文文档
3.1.5发布于 2017/12/13MITCommonJS近 30 天下载 6211

Shared functionality for websocket providers

原始使用文档
文档目录7 节
  1. About
  2. Channels
  3. Documentation
  4. app.channel(... names)
  5. app.service('servicename').publish(event, callback) , app.s…
  6. app.on('publish', function(event, channel, hook) {})
  7. License

@feathersjs/socket-commons

Greenkeeper badge

Build Status Test Coverage Dependency Status Download Status

Shared functionality for websocket providers like @feathers/socketio and @feathersjs/primus. Only intended to be used internally by other Feathers real-time providers.

About

@feathersjs/socket-commons contains internal shared functionality for Feathers real-time providers (currently Socket.io and Primus).

lib/client.js is a base socket service client lib/index.js returns a configurable function and requires the following options:

  • done - A Promise that resolves once the real-time protocol server has been set up
  • emit - The name of the method to emit data to a socket ('emit' for Socket.io and 'send' for Primus)
  • socketKey - A string or ES6 Symbol which stores the actual socket connection
  • getParams - A function that returns the Feathers connection options for a socket

Channels

Channels provide channel functionality for bi-directional Feathers service providers. It is e.g. used by the Socket.io and Primus provider to quickly determine what messages to send to connected clients.

const channels = require('@feathersjs/socket-commons/lib/channels');

Documentation

app.channel(... names)

Returns a named or combined channel object.

const channel = app.channel('test'); // return a `test` channel

channel.join(connection); // join a channel
channel.leave(connection); // leave a channel

channel.filter(connection => {}) // return a new channel with filtered connections
channel.length // return the number of connections
channel.connections // all connections in this channel

const combined = app.channel('test', 'other'); // return a combined channel

combined.join(connection); // join the `test` and `other` channel
combined.leave(connection); // leave the `test` and `other` channel

channel.filter(connection => {}) // return a new channel with filtered connections (connections will only be iterated once)
combined.length // return the number of connections
combined.connections // all connections in the combined channel (if a connection is in multiple channels it will only show once)

app.service('servicename').publish(event, callback), app.service('servicename').publish(callback)

Register a publishing callback for a service and event (or all events) that returns a (named or combined) channel.

app.use('/test', {
  create(data) {
    return Promise.resolve(data);
  }
});

// `created` event for the `test` service
app.service('test').publish('created', (data, hook) =>
  app.channel('*')
);

// `created` event for the `test` service, sending different data to two different channels
app.service('test').publish('created', (data, hook) => {
  return [
    app.channel('admins'),
    app.channel('users').send(_.omit(data, 'groups', 'email'))
  ];
});

// All events for all services
app.publish((data, hook) =>
  app.channel('*')
);

// All `created` events for all services
app.publish('created', (data, hook) =>
  app.channel('*')
);

// All events for `test` service
app.service('test').publish((data, hook) =>
  app.channel('*')
);

app.on('publish', function(event, channel, hook) {})

An event that will be sent every time a service event that has connections to publish to happens. channel is a combined channel with all connections to publish the event to.

Note: If there are no channels or connections the publish event will not be sent.

app.on('publish', (event, channel, hook) => {
  channel.connections.forEach(connection => {
    // Do something with `connection`
  });
});

License

Copyright (c) 2015

Licensed under the MIT license.

版本与发布记录

@feathersjs/socket-commons 共发布 10 个版本,首次发布于 2017/10/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.1.5latest2017/12/138 年前
3.1.42017/12/138 年前
3.1.32017/12/138 年前
3.1.22017/12/078 年前
3.1.12017/12/078 年前
3.1.02017/12/078 年前

其他发布通道:pre → 3.0.0-pre.7。

关于 @feathersjs/socket-commons 的常见问题

@feathersjs/socket-commons 是什么?

Shared functionality for websocket providers

如何安装 @feathersjs/socket-commons?

在项目目录运行 npm install @feathersjs/socket-commons,或使用 pnpm add / yarn add / bun add。

@feathersjs/socket-commons 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/feathersjs__socket-commons 是否存在。

@feathersjs/socket-commons 支持 ESM 还是 CommonJS?

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

@feathersjs/socket-commons 需要什么版本的 Node.js?

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

@feathersjs/socket-commons 可以商用吗?

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

@feathersjs/socket-commons 的下载量有多少?

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

@feathersjs/socket-commons 还在维护吗?

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