Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK] Add host, wsPort, and wssPort configurations #165

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 45 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,25 @@ a minimal application to connect to a channel and send events.
- [Android specific installation](#android-specific-installation)
- [Initialization](#initialization)
- [Configuration](#configuration)
- [`activityTimeout (double)`](#activitytimeout-double)
- [`apiKey (string)`](#apikey-string)
- [`authEndpoint (string)`](#authendpoint-string)
- [`cluster (string)`](#cluster-string)
- [`useTLS (bool)`](#usetls-bool)
- [`activityTimeout (double)`](#activitytimeout-double)
- [`apiKey (string)`](#apikey-string)
- [`authEndpoint (string)`](#authendpoint-string)
- [`host (string)`](#host-string)
- [`wsPort (string)`](#wsPort-number)
- [`wssPort (string)`](#wssPort-number)
- [`cluster (string)`](#cluster-string)
- [`useTLS (bool)`](#usetls-bool)
- [Event Callback parameters](#event-callback-parameters)
- [`onEvent`](#onevent)
- [`onSubscriptionSucceeded`](#onsubscriptionsucceeded)
- [`onSubscriptionError`](#onsubscriptionerror)
- [`onDecryptionFailure`](#ondecryptionfailure)
- [`onSubscriptionCount`](#onsubscriptioncount)
- [`onMemberAdded`](#onmemberadded)
- [`onMemberRemoved`](#onmemberremoved)
- [`onAuthorizer`](#onauthorizer)
- [`onConnectionStateChange`](#onconnectionstatechange)
- [`onError`](#onerror)
- [`onEvent`](#onevent)
- [`onSubscriptionSucceeded`](#onsubscriptionsucceeded)
- [`onSubscriptionError`](#onsubscriptionerror)
- [`onDecryptionFailure`](#ondecryptionfailure)
- [`onSubscriptionCount`](#onsubscriptioncount)
- [`onMemberAdded`](#onmemberadded)
- [`onMemberRemoved`](#onmemberremoved)
- [`onAuthorizer`](#onauthorizer)
- [`onConnectionStateChange`](#onconnectionstatechange)
- [`onError`](#onerror)
- [Connection handling](#connection-handling)
- [Connecting](#connecting)
- [Disconnecting](#disconnecting)
Expand Down Expand Up @@ -153,18 +156,21 @@ You can subscribe to channels before calling `connect()`.
There are a few configuration parameters which can be set for the Pusher client. The following table
describes available parameters for each platform:

| parameter | Android | iOS |
| -------------------------- | ------- | --- |
| activityTimeout | ✅ | ✅ |
| apiKey | ✅ | ✅ |
| authEndpoint | ✅ | ✅ |
| cluster | ✅ | ✅ |
| maxReconnectGapInSeconds | ✅ | ✅ |
| maxReconnectionAttempts | ✅ | ✅ |
| pongTimeout | ✅ | ✅ |
| proxy | ✅ | ⬜️ |
| useTLS | ✅ | ✅ |
| authorizerTimeoutInSeconds | ⬜️ | ✅ |
| Parameter | Android | iOS |
|----------------------------|:-------:|:---:|
| activityTimeout | ✅ | ✅ |
| apiKey | ✅ | ✅ |
| authEndpoint | ✅ | ✅ |
| host | ✅ | ✅ |
| wsPort | ✅ | ✅ |
| wssPort | ✅ | ✅ |
| cluster | ✅ | ✅ |
| maxReconnectGapInSeconds | ✅ | ✅ |
| maxReconnectionAttempts | ✅ | ✅ |
| pongTimeout | ✅ | ✅ |
| proxy | ✅ | ⬜️ |
| useTLS | ✅ | ✅ |
| authorizerTimeoutInSeconds | ⬜️ | ✅ |

#### `activityTimeout (double)`

Expand All @@ -181,6 +187,18 @@ Pusher client will call to authorize users
for a presence channel. Learn [how to implement
an authorization service](https://pusher.com/docs/channels/server_api/authenticating-users/)

#### `host (string)`

Specifies the host that pusher-js should connect to. If you do not specify a host, pusher's default host will be used by default.

#### `wsPort (number)`

Specifies the wsPort that pusher-js should connect to. If you do not specify a wsPort, If you do not specify a wsPort, `80` will be used by default.

#### `wssPort (number)`

Specifies the wssPort that pusher-js should connect to. If you do not specify a wssPort, If you do not specify a wssPort, `443` will be used by default.

#### `cluster (string)`

Specifies the cluster that pusher-js should connect to. Here's the full list of [Pusher clusters](https://pusher.com/docs/clusters). If you do not specify a cluster, `mt1` will be used by default.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
pusher!!.disconnect()
}
val options = PusherOptions()
if (arguments.hasKey("host")) options.setHost(arguments.getString("host"))
if (arguments.hasKey("wsPort")) options.setWsPort(arguments.getInt("wsPort"))
if (arguments.hasKey("wssPort")) options.setWssPort(arguments.getInt("wssPort"))
if (arguments.hasKey("cluster")) options.setCluster(arguments.getString("cluster"))
if (arguments.hasKey("useTLS")) options.isUseTLS =
arguments.getBoolean("useTLS")
Expand Down
36 changes: 36 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export default function App() {
const pusher = Pusher.getInstance();

const [apiKey, onChangeApiKey] = React.useState('');
const [host, onChangeHost] = React.useState('');
const [wsPort, onChangeWsPort] = React.useState(80);
const [wssPort, onChangeWssPort] = React.useState(443);
const [cluster, onChangeCluster] = React.useState('');
const [channelName, onChangeChannelName] = React.useState('');
const [eventName, onChangeEventName] = React.useState('');
Expand All @@ -40,6 +43,9 @@ export default function App() {
React.useEffect(() => {
const getFromStorage = async () => {
onChangeApiKey((await AsyncStorage.getItem('APIKEY')) || '');
onChangeHost((await AsyncStorage.getItem('HOST')) || '');
onChangeWsPort((await AsyncStorage.getItem('WS_PORT')) || '');
onChangeWssPort((await AsyncStorage.getItem('WSS_PORT')) || '');
onChangeCluster((await AsyncStorage.getItem('CLUSTER')) || '');
onChangeChannelName((await AsyncStorage.getItem('CHANNEL')) || '');
onChangeEventName((await AsyncStorage.getItem('EVENT')) || '');
Expand All @@ -54,12 +60,18 @@ export default function App() {
try {
await AsyncStorage.multiSet([
['APIKEY', apiKey],
['HOST', host],
['WS_PORT', wsPort],
['WSS_PORT', wssPort],
['CLUSTER', cluster],
['CHANNEL', channelName],
]);

await pusher.init({
apiKey,
host,
wsPort,
wssPort,
cluster,
// authEndpoint
// ============
Expand Down Expand Up @@ -226,6 +238,30 @@ export default function App() {
autoCapitalize="none"
value={apiKey}
/>
<TextInput
style={styles.input}
onChangeText={onChangeHost}
value={host}
placeholder="Host"
autoCapitalize="none"
keyboardType="default"
/>
<TextInput
style={styles.input}
onChangeText={onChangeWsPort}
value={wsPort}
placeholder="80"
autoCapitalize="none"
keyboardType="default"
/>
<TextInput
style={styles.input}
onChangeText={onChangeWssPort}
value={wssPort}
placeholder="443"
autoCapitalize="none"
keyboardType="default"
/>
<TextInput
style={styles.input}
onChangeText={onChangeCluster}
Expand Down
16 changes: 10 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ export class Pusher {

public init(args: {
apiKey: string;
cluster: string;
host?: string;
wsPort?: Number;
wssPort?: Number;
cluster?: string;
authEndpoint?: string;
useTLS?: boolean;
activityTimeout?: Number;
Expand Down Expand Up @@ -197,11 +200,11 @@ export class Pusher {
const data = event.data;
const userId = event.userId;
const channel = this.channels.get(channelName);
// Depending on the platform implementation we get json or a Map.
const decodedData = data instanceof Object ? data : JSON.parse(data);

switch (eventName) {
case 'pusher_internal:subscription_succeeded':
// Depending on the platform implementation we get json or a Map.
var decodedData = data instanceof Object ? data : JSON.parse(data);
for (const _userId in decodedData?.presence?.hash) {
const userInfo = decodedData?.presence?.hash[_userId];
var member = new PusherMember(_userId, userInfo);
Expand All @@ -214,8 +217,6 @@ export class Pusher {
channel?.onSubscriptionSucceeded?.(decodedData);
break;
case 'pusher_internal:subscription_count':
// Depending on the platform implementation we get json or a Map.
var decodedData = data instanceof Object ? data : JSON.parse(data);
if (channel) {
channel.subscriptionCount = decodedData.subscription_count;
}
Expand Down Expand Up @@ -276,6 +277,9 @@ export class Pusher {

return PusherWebsocketReactNative.initialize({
apiKey: args.apiKey,
host: args.host,
wsPort: args.wsPort,
wssPort: args.wssPort,
cluster: args.cluster,
authEndpoint: args.authEndpoint,
useTLS: args.useTLS,
Expand All @@ -284,7 +288,7 @@ export class Pusher {
maxReconnectionAttempts: args.maxReconnectionAttempts,
maxReconnectGapInSeconds: args.maxReconnectGapInSeconds,
authorizerTimeoutInSeconds: args.authorizerTimeoutInSeconds,
authorizer: args.onAuthorizer ? true : false,
authorizer: !!args.onAuthorizer,
proxy: args.proxy,
});
}
Expand Down