Skip to content

Commit

Permalink
Prepare 4.0.0 (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia authored Jun 29, 2023
1 parent 14da627 commit 1434e28
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ jobs:
- name: Release on npm
run: |
npm config set //registry.npmjs.org/:_authToken ${NPM_TOKEN}
npm publish --ignore-scripts --tag beta
npm publish --ignore-scripts
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
4.0.0
=====

This release changes the semantics of working with connection tokens described in [Centrifugo v5 release post](https://centrifugal.dev/blog/2023/06/29/centrifugo-v5-released#token-behaviour-adjustments-in-sdks).

Previously, returning an empty token string from `getToken` callback resulted in client disconnection with unauthorized reason.

Now returning an empty string from `getToken` is a valid scenario which won't result into disconnect on the client side. It's still possible to disconnect client by throwing a special `UnauthorizedError()` error from `getToken` function.

And we are putting back `setToken` method to the SDK – so it's now possible to reset the token to be empty upon user logout.

Also, centrifuge-js v4 contains the reworked reconnect code where we schedule reconnect task from the disconnect function instead of transport close event handler - see [#234](https://github.com/centrifugal/centrifuge-js/pull/234).

3.1.2
=====

Expand Down
89 changes: 38 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The client behaves according to a common [Centrifigo SDK spec](https://centrifug

The features implemented by this SDK can be found in [SDK feature matrix](https://centrifugal.dev/docs/transports/client_sdk#sdk-feature-matrix).

> **`centrifuge-js` v3.x is compatible only with latest [Centrifugo](https://github.com/centrifugal/centrifugo) server (v4) and [Centrifuge](https://github.com/centrifugal/centrifuge) >= 0.25.0. For Centrifugo v2, Centrifugo v3 and Centrifuge < 0.25.0 you should use `centrifuge-js` v2.x.**
> **`centrifuge-js` v4.x is compatible with [Centrifugo](https://github.com/centrifugal/centrifugo) server v4 and v5 and [Centrifuge](https://github.com/centrifugal/centrifuge) >= 0.25.0. For Centrifugo v2, Centrifugo v3 and Centrifuge < 0.25.0 you should use `centrifuge-js` v2.x.**
* [Install](#install)
* [Quick start](#quick-start)
Expand Down Expand Up @@ -41,10 +41,10 @@ And then in your project:
import { Centrifuge } from 'centrifuge';
```

In browser, you can import SDK from CDN (replace `3.1.0` to a concrete version number you want to use, see [releases](https://github.com/centrifugal/centrifuge-js/releases)):
In browser, you can import SDK from CDN (replace `4.0.0` with a concrete version number you want to use, see [releases](https://github.com/centrifugal/centrifuge-js/releases)):

```html
<script src="https://unpkg.com/centrifuge@3.1.0/dist/centrifuge.js"></script>
<script src="https://unpkg.com/centrifuge@4.0.0/dist/centrifuge.js"></script>
```

See also [centrifuge-js on cdnjs](https://cdnjs.com/libraries/centrifuge). Note that `centrifuge-js` browser builds target [ES6](https://caniuse.com/es6) at this point.
Expand Down Expand Up @@ -142,7 +142,7 @@ If you want to use SockJS you must also import SockJS client before centrifuge.j

```html
<script src="https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js" type="text/javascript"></script>
<script src="https://unpkg.com/centrifuge@3.1.0/dist/centrifuge.js" type="text/javascript"></script>
<script src="https://unpkg.com/centrifuge@4.0.0/dist/centrifuge.js" type="text/javascript"></script>
```

Or provide it explicitly as a dependency:
Expand Down Expand Up @@ -338,35 +338,28 @@ If the token sets connection expiration then the client SDK will keep the token
An example of possible `getToken` function implementation:

```javascript
function getToken(url, ctx) {
return new Promise((resolve, reject) => {
fetch(url, {
method: 'POST',
headers: new Headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify(ctx)
})
.then(res => {
if (!res.ok) {
throw new Error(`Unexpected status code ${res.status}`);
}
return res.json();
})
.then(data => {
resolve(data.token);
})
.catch(err => {
reject(err);
});
});
async function getToken() {
if (!loggedIn) {
return "";
}
const res = await fetch('/centrifuge/connection_token');
if (!res.ok) {
if (res.status === 403) {
// Return special error to not proceed with token refreshes, client will be disconnected.
throw new Centrifuge.UnauthorizedError();
}
// Any other error thrown will result into token refresh re-attempts.
throw new Error(`Unexpected status code ${res.status}`);
}
const data = await res.json();
return data.token;
}

const client = new Centrifuge(
'ws://localhost:8000/connection/websocket',
{
token: 'JWT-GENERATED-ON-BACKEND-SIDE',
getToken: function (ctx) {
return getToken('/centrifuge/connection_token', ctx);
}
getToken: getToken
}
);
```
Expand Down Expand Up @@ -551,36 +544,30 @@ If token sets subscription expiration client SDK will keep token refreshed. It d
An example:

```javascript
function getToken(url, ctx) {
return new Promise((resolve, reject) => {
fetch(url, {
method: 'POST',
headers: new Headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify(ctx)
})
.then(res => {
if (!res.ok) {
throw new Error(`Unexpected status code ${res.status}`);
}
return res.json();
})
.then(data => {
resolve(data.token);
})
.catch(err => {
reject(err);
});
async function getToken(ctx) {
// ctx argument has a channel.
const res = await fetch('/centrifuge/subscription_token', {
method: 'POST',
headers: new Headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify(ctx)
});
if (!res.ok) {
if (res.status === 403) {
// Return special error to not proceed with token refreshes, subscription will be unsubscribed.
throw new Centrifuge.UnauthorizedError();
}
// Any other error thrown will result into token refresh re-attempts.
throw new Error(`Unexpected status code ${res.status}`);
}
const data = await res.json();
return data.token;
}

const client = new Centrifuge('ws://localhost:8000/connection/websocket', {});

const sub = centrifuge.newSubscription(channel, {
token: 'JWT-GENERATED-ON-BACKEND-SIDE',
getToken: function (ctx) {
// ctx has channel in the Subscription token case.
return getToken('/centrifuge/subscription_token', ctx);
},
getToken: getToken,
});
sub.subscribe();
```
Expand Down Expand Up @@ -702,7 +689,7 @@ Timeout for operations in milliseconds.
To import client with Protobuf protocol support:

```html
<script src="https://unpkg.com/centrifuge@3.0.0/dist/centrifuge.protobuf.js"></script>
<script src="https://unpkg.com/centrifuge@4.0.0/dist/centrifuge.protobuf.js"></script>
```

Or if you are developing with npm:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "centrifuge",
"version": "3.2.0-beta.0",
"version": "4.0.0",
"description": "JavaScript client SDK for bidirectional communication with Centrifugo and Centrifuge-based server from browser, NodeJS and React Native",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down

0 comments on commit 1434e28

Please sign in to comment.