Skip to content

Commit

Permalink
propagate the PB_CONNECT realtime event
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Dec 11, 2023
1 parent 6046929 commit 7c100bb
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 14 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 0.20.1

- Propagate the `PB_CONNECT` event to allow listening to the realtime connect/reconnect events.
```js
pb.realtime.subscribe("PB_CONNECT", (e) => {
console.log(e.clientId);
})
```

## 0.20.0

- Added `expand`, `filter`, `fields`, custom query and headers parameters support for the realtime subscriptions.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,8 @@ const pb = new PocketBase(baseUrl = '/', authStore = LocalAuthStore);
```js
// Initialize the realtime connection (if not already) and register the subscription listener.
//
// You can subscribe to the `PB_CONNECT` event if you want to listen to the realtime connection connect/reconnect events.
🔓 pb.realtime.subscribe(topic, callback, options = {});

// Unsubscribe from all subscription listeners with the specified topic.
Expand Down
2 changes: 1 addition & 1 deletion dist/pocketbase.cjs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.cjs.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.mjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.iife.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.iife.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.umd.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.20.0",
"version": "0.20.1",
"name": "pocketbase",
"description": "PocketBase JavaScript SDK",
"author": "Gani Georgiev",
Expand Down
13 changes: 10 additions & 3 deletions src/services/RealtimeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,11 @@ export class RealtimeService extends BaseService {
this.connectErrorHandler(new Error("Failed to establish realtime connection."));
};

this.eventSource.addEventListener('PB_CONNECT', (e) => {
this.eventSource.addEventListener("PB_CONNECT", (e) => {
const msgEvent = (e as MessageEvent);
this.clientId = msgEvent?.lastEventId;

this.submitSubscriptions()
.then(async () => {
this.submitSubscriptions().then(async () => {
let retries = 3;
while (this.hasUnsentSubscriptions() && retries > 0) {
retries--;
Expand All @@ -386,6 +385,14 @@ export class RealtimeService extends BaseService {
this.reconnectAttempts = 0;
clearTimeout(this.reconnectTimeoutId);
clearTimeout(this.connectTimeoutId);

// propagate the PB_CONNECT event
const connectSubs = this.getSubscriptionsByTopic("PB_CONNECT");
for (let key in connectSubs) {
for (let listener of connectSubs[key]) {
listener(e);
}
}
}).catch((err) => {
this.clientId = "";
this.connectErrorHandler(err);
Expand Down

0 comments on commit 7c100bb

Please sign in to comment.