Skip to content

Commit f2e3d16

Browse files
Initial commit
0 parents  commit f2e3d16

File tree

15 files changed

+4880
-0
lines changed

15 files changed

+4880
-0
lines changed

.github/workflows/ci.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: '0 0 * * 0'
8+
9+
jobs:
10+
test-node:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node-version: [12.x, 14.x, 16.x]
16+
17+
services:
18+
postgres:
19+
image: postgres
20+
env:
21+
POSTGRES_PASSWORD: changeit
22+
options: >-
23+
--health-cmd pg_isready
24+
--health-interval 10s
25+
--health-timeout 5s
26+
--health-retries 5
27+
ports:
28+
- 5432:5432
29+
30+
steps:
31+
- uses: actions/checkout@v2
32+
- name: Use Node.js ${{ matrix.node-version }}
33+
uses: actions/setup-node@v1
34+
with:
35+
node-version: ${{ matrix.node-version }}
36+
37+
- run: npm ci
38+
39+
- run: npm test
40+
env:
41+
CI: true
42+
timeout-minutes: 10

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
dist/
3+
.nyc_output/

CHANGELOG.md

Whitespace-only changes.

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2021 Damien Arrachequesne (@darrachequesne)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Socket.IO Postgres emitter
2+
3+
The `@socket.io/postgres-emitter` package allows you to easily communicate with a group of Socket.IO servers from another Node.js process (server-side).
4+
5+
![Emitter diagram](./assets/emitter.png)
6+
7+
It must be used in conjunction with [`@socket.io/posgres-adapter`](https://github.com/socketio/socket.io-posgres-adapter/).
8+
9+
Supported features:
10+
11+
- [broadcasting](https://socket.io/docs/v4/broadcasting-events/)
12+
- [utility methods](https://socket.io/docs/v4/server-instance/#Utility-methods)
13+
- [`socketsJoin`](https://socket.io/docs/v4/server-instance/#socketsJoin)
14+
- [`socketsLeave`](https://socket.io/docs/v4/server-instance/#socketsLeave)
15+
- [`disconnectSockets`](https://socket.io/docs/v4/server-instance/#disconnectSockets)
16+
- [`serverSideEmit`](https://socket.io/docs/v4/server-instance/#serverSideEmit)
17+
18+
**Table of contents**
19+
20+
- [Installation](#installation)
21+
- [Usage](#usage)
22+
- [API](#api)
23+
- [Known errors](#known-errors)
24+
- [License](#license)
25+
26+
## Installation
27+
28+
```
29+
npm install @socket.io/postgres-emitter pg
30+
```
31+
32+
For TypeScript users, you might also need `@types/pg`.
33+
34+
## Usage
35+
36+
```js
37+
const { Emitter } = require("@socket.io/postgres-emitter");
38+
const { Pool } = require("pg");
39+
40+
const pool = new Pool({
41+
user: "postgres",
42+
host: "localhost",
43+
database: "postgres",
44+
password: "changeit",
45+
port: 5432,
46+
});
47+
48+
const io = new Emitter(pool);
49+
50+
setInterval(() => {
51+
io.emit("ping", new Date());
52+
}, 1000);
53+
```
54+
55+
## API
56+
57+
### `Emitter(pool[, nsp][, opts])`
58+
59+
```js
60+
const io = new Emitter(pool);
61+
```
62+
63+
The `pool` argument is a [Pool object](https://node-postgres.com/api/pool) from the `pg` package.
64+
65+
### `Emitter#to(room:string):BroadcastOperator`
66+
### `Emitter#in(room:string):BroadcastOperator`
67+
68+
Specifies a specific `room` that you want to emit to.
69+
70+
```js
71+
io.to("room1").emit("hello");
72+
```
73+
74+
### `Emitter#except(room:string):BroadcastOperator`
75+
76+
Specifies a specific `room` that you want to exclude from broadcasting.
77+
78+
```js
79+
io.except("room2").emit("hello");
80+
```
81+
82+
### `Emitter#of(namespace:string):Emitter`
83+
84+
Specifies a specific namespace that you want to emit to.
85+
86+
```js
87+
const customNamespace = io.of("/custom");
88+
89+
customNamespace.emit("hello");
90+
```
91+
92+
### `Emitter#socketsJoin(rooms:string|string[])`
93+
94+
Makes the matching socket instances join the specified rooms:
95+
96+
```js
97+
// make all Socket instances join the "room1" room
98+
io.socketsJoin("room1");
99+
100+
// make all Socket instances of the "admin" namespace in the "room1" room join the "room2" room
101+
io.of("/admin").in("room1").socketsJoin("room2");
102+
```
103+
104+
### `Emitter#socketsLeave(rooms:string|string[])`
105+
106+
Makes the matching socket instances leave the specified rooms:
107+
108+
```js
109+
// make all Socket instances leave the "room1" room
110+
io.socketsLeave("room1");
111+
112+
// make all Socket instances of the "admin" namespace in the "room1" room leave the "room2" room
113+
io.of("/admin").in("room1").socketsLeave("room2");
114+
```
115+
116+
### `Emitter#disconnectSockets(close:boolean)`
117+
118+
Makes the matching socket instances disconnect:
119+
120+
```js
121+
// make all Socket instances disconnect
122+
io.disconnectSockets();
123+
124+
// make all Socket instances of the "admin" namespace in the "room1" room disconnect
125+
io.of("/admin").in("room1").disconnectSockets();
126+
127+
// this also works with a single socket ID
128+
io.of("/admin").in(theSocketId).disconnectSockets();
129+
```
130+
131+
### `Emitter#serverSideEmit(ev:string[,...args:any[]])`
132+
133+
Emits an event that will be received by each Socket.IO server of the cluster.
134+
135+
```js
136+
io.serverSideEmit("ping");
137+
```
138+
139+
## License
140+
141+
[MIT](LICENSE)

0 commit comments

Comments
 (0)