# ws-event-bus

> event bus between frontend and backend

Latest version **1.2.1** (published 2022-12-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install ws-event-bus
pnpm add ws-event-bus
yarn add ws-event-bus
bun add ws-event-bus
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2022-12-12 |
| First published | 2022-08-31 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 179.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | wuhao |
| Maintainers | whyymj |
| Keywords | event-bus, ws, development |

## Links

- npm: https://www.npmjs.com/package/ws-event-bus
- Repository: https://gitee.com/whyymj/allend-event-bus
- npm.io page: https://npm.io/package/ws-event-bus

## Dependencies (6)

- [ws](https://npm.io/package/ws.md) ^8.8.1
- [http](https://npm.io/package/http.md) ^0.0.1-security
- [koa2](https://npm.io/package/koa2.md) ^2.0.0-alpha.7
- [koa-route](https://npm.io/package/koa-route.md) ^3.2.0
- [koa-static](https://npm.io/package/koa-static.md) ^5.0.0
- [koa-websocket](https://npm.io/package/koa-websocket.md) ^7.0.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.2.1 (latest) — 2022-12-12
- 1.2.0 — 2022-11-24
- 1.1.23 — 2022-09-30
- 1.1.22 — 2022-09-14
- 1.1.21 — 2022-09-14
- 1.1.20 — 2022-09-14
- 1.1.19 — 2022-09-14
- 1.1.18 — 2022-09-14
- 1.1.17 — 2022-09-07
- 1.1.16 — 2022-09-07
- 1.1.15 — 2022-09-06
- 1.1.14 — 2022-09-06
- 1.1.13 — 2022-09-04
- 1.1.12 — 2022-09-04
- 1.1.11 — 2022-09-02
- … 18 more at https://npm.io/package/ws-event-bus/versions

## README

# ws-event-bus

#### 介绍
开发时使用的event bus;利用ws服务器作为中转。

#### 安装教程

npm i ws -D

npm i ws-event-bus -D

#### 使用说明

```
浏览器端：

html:
<body>
    <button id="btn1">btn1</button> 
    <button id="btn2">btn2</button> 
</body>


js（需要打包）:

import WSS from 'ws-event-bus'

function getClient(id) {
  let ws = new WSS({
    id,
    url: 'ws://localhost:8080/'
  })
  ws.on('message', (data) => {
    console.log(id + ' get message >>>>', data)
  })
  ws.on('wait', (data,response) => {
    console.log(id + ' wait >>>>', response)
  })
  return ws
}
let w1 = getClient('w1')
let w2 = getClient('w2')

function click1(){
  w1.send('this is w1+', {
    to: 'w2'
  }).then(res => {
    console.log('w1 get reply')
  })
}
function click2(){
  w2.reply(w2.waiting[0],'value')
}
 
document.getElementById('btn1').onclick = click1
document.getElementById('btn2').onclick = click2

```



```服务端：

server:

const WSS = require('ws-event-bus/dist/server.js').default
const path = require('path')
let server = new Server({
    port:8080
})
server.static(path.resolve(process.cwd()),'./');//静态资源
server.start()

```


```
nodejs:

const WSS = require('ws-event-bus').default
function getClient(id) {
  let ws = new WSS({
    id,
    url: 'ws://localhost:8080/'
  })
  ws.on('message', (data) => {
    console.log(id + ' get message>>>>', data)
  })
  ws.on('wait', (data, response) => {
    console.log(id + ' wait >>>>', response)
  })

  return ws
}
let w1 = getClient('w1')
let w2 = getClient('w2')

function click1() {
  w1.send('this is w1+', {
    to: 'w2'
  }).then(res => {
    console.log('w1 sucewss++++', res)
  })
}

function click2() {
  w2.reply(w2.waiting[0],'value')
}


click1()

setTimeout(() => {
  click2()
}, 3000)
setTimeout(() => {}, 100000)

```

---
_Source: https://npm.io/package/ws-event-bus · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
