Skip to content

Commit f104d28

Browse files
Merge remote-tracking branch 'socket.io-postgres-emitter/main' into monorepo
2 parents a66ed68 + d4ed6cb commit f104d28

File tree

14 files changed

+4859
-0
lines changed

14 files changed

+4859
-0
lines changed

.github/workflows/ci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ jobs:
3535
ports:
3636
- 6379:6379
3737

38+
postgres:
39+
image: postgres
40+
env:
41+
POSTGRES_PASSWORD: changeit
42+
options: >-
43+
--health-cmd pg_isready
44+
--health-interval 10s
45+
--health-timeout 5s
46+
--health-retries 5
47+
ports:
48+
- 5432:5432
49+
3850
steps:
3951
- name: Checkout repository
4052
uses: actions/checkout@v4
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 0.1.0 (2021-06-14)
2+
3+
Initial commit
4+
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.
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
Related packages:
19+
20+
- Postgres adapter: https://github.com/socketio/socket.io-postgres-adapter/
21+
- Redis adapter: https://github.com/socketio/socket.io-redis-adapter/
22+
- Redis emitter: https://github.com/socketio/socket.io-redis-emitter/
23+
- MongoDB adapter: https://github.com/socketio/socket.io-mongo-adapter/
24+
- MongoDB emitter: https://github.com/socketio/socket.io-mongo-emitter/
25+
26+
**Table of contents**
27+
28+
- [Installation](#installation)
29+
- [Usage](#usage)
30+
- [API](#api)
31+
- [Known errors](#known-errors)
32+
- [License](#license)
33+
34+
## Installation
35+
36+
```
37+
npm install @socket.io/postgres-emitter pg
38+
```
39+
40+
For TypeScript users, you might also need `@types/pg`.
41+
42+
## Usage
43+
44+
```js
45+
const { Emitter } = require("@socket.io/postgres-emitter");
46+
const { Pool } = require("pg");
47+
48+
const pool = new Pool({
49+
user: "postgres",
50+
host: "localhost",
51+
database: "postgres",
52+
password: "changeit",
53+
port: 5432,
54+
});
55+
56+
const io = new Emitter(pool);
57+
58+
setInterval(() => {
59+
io.emit("ping", new Date());
60+
}, 1000);
61+
```
62+
63+
## API
64+
65+
### `Emitter(pool[, nsp][, opts])`
66+
67+
```js
68+
const io = new Emitter(pool);
69+
```
70+
71+
The `pool` argument is a [Pool object](https://node-postgres.com/api/pool) from the `pg` package.
72+
73+
### `Emitter#to(room:string):BroadcastOperator`
74+
### `Emitter#in(room:string):BroadcastOperator`
75+
76+
Specifies a specific `room` that you want to emit to.
77+
78+
```js
79+
io.to("room1").emit("hello");
80+
```
81+
82+
### `Emitter#except(room:string):BroadcastOperator`
83+
84+
Specifies a specific `room` that you want to exclude from broadcasting.
85+
86+
```js
87+
io.except("room2").emit("hello");
88+
```
89+
90+
### `Emitter#of(namespace:string):Emitter`
91+
92+
Specifies a specific namespace that you want to emit to.
93+
94+
```js
95+
const customNamespace = io.of("/custom");
96+
97+
customNamespace.emit("hello");
98+
```
99+
100+
### `Emitter#socketsJoin(rooms:string|string[])`
101+
102+
Makes the matching socket instances join the specified rooms:
103+
104+
```js
105+
// make all Socket instances join the "room1" room
106+
io.socketsJoin("room1");
107+
108+
// make all Socket instances of the "admin" namespace in the "room1" room join the "room2" room
109+
io.of("/admin").in("room1").socketsJoin("room2");
110+
```
111+
112+
### `Emitter#socketsLeave(rooms:string|string[])`
113+
114+
Makes the matching socket instances leave the specified rooms:
115+
116+
```js
117+
// make all Socket instances leave the "room1" room
118+
io.socketsLeave("room1");
119+
120+
// make all Socket instances of the "admin" namespace in the "room1" room leave the "room2" room
121+
io.of("/admin").in("room1").socketsLeave("room2");
122+
```
123+
124+
### `Emitter#disconnectSockets(close:boolean)`
125+
126+
Makes the matching socket instances disconnect:
127+
128+
```js
129+
// make all Socket instances disconnect
130+
io.disconnectSockets();
131+
132+
// make all Socket instances of the "admin" namespace in the "room1" room disconnect
133+
io.of("/admin").in("room1").disconnectSockets();
134+
135+
// this also works with a single socket ID
136+
io.of("/admin").in(theSocketId).disconnectSockets();
137+
```
138+
139+
### `Emitter#serverSideEmit(ev:string[,...args:any[]])`
140+
141+
Emits an event that will be received by each Socket.IO server of the cluster.
142+
143+
```js
144+
io.serverSideEmit("ping");
145+
```
146+
147+
## License
148+
149+
[MIT](LICENSE)

0 commit comments

Comments
 (0)