Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/OrdinarySF/capacitor-websocket-client

Capacitor WebSocket Client Plugin.
https://github.com/OrdinarySF/capacitor-websocket-client

adroid capacitor capacitor-plugin ionic websocket

Last synced: about 2 months ago
JSON representation

Capacitor WebSocket Client Plugin.

Awesome Lists containing this project

README

        

# @wahr/capacitor-websocket-client

[![Downloads][badge-dl]][download]
[![License][badge-license]][license]
[![Issues][badge-issues]][issues]
[![Version][badge-version]][download]

[badge-dl]: https://img.shields.io/npm/dw/%40wahr%2Fcapacitor-websocket-client?style=flat-square
[download]: https://www.npmjs.com/package/@wahr/capacitor-websocket-client?activeTab=versions
[badge-license]: https://img.shields.io/npm/l/%40wahr%2Fcapacitor-websocket-client?style=flat-square
[license]: https://github.com/OrdinarySF/capacitor-websocket-client/blob/main/LICENSE
[badge-issues]: https://img.shields.io/github/issues/OrdinarySF/capacitor-websocket-client?style=flat-square
[issues]: https://github.com/OrdinarySF/capacitor-websocket-client/issues
[badge-version]: https://img.shields.io/npm/v/%40wahr%2Fcapacitor-websocket-client?style=flat-square

Capacitor WebSocket Client Plugin.

## Install

```bash
npm install @wahr/capacitor-websocket-client
npx cap sync
```

## Platform support

- Web
- Android

> Unfortunately, we do not have a macOS device, but we are working hard.

## Example

#### Single connect

```typescript
await WebSocket.onOpen({}, (message, err) => {
//do something...
console.log("onOpen event have a bug: ", err?.toString())
})

await WebSocket.onMessage({}, (message, err) => {
//do something...
console.log(`received message content: ${message?.data}`)
})

await WebSocket.connect({url: "ws://example.com"})

setTimeout(async () => {
await WebSocket.send({data: "hello world!"})
}, 2000);
```

#### Multiple connect

```typescript
await WebSocket.onOpen({id: "chat-websocket"}, (message, err) => {
//do something...
console.log("onOpen event have a bug: ", err?.toString())
})

await WebSocket.connect({url: "ws://example.com/chat", id: "chat-websocket"})

await WebSocket.onMessage({id: "notify-websocket"}, (message, err) => {
//do something...
console.log(`received notify content: ${message?.data}`)
})

await WebSocket.connect({url: "ws://example.com/notify", id: "notify-websocket"})

setTimeout(async () => {
await WebSocket.send({data: "hello world!", id: "chat-websocket"})
await WebSocket.send({data: "connect notify.", id: "notify-websocket"})
}, 2000)
```

## API

* [`connect(...)`](#connect)
* [`close(...)`](#close)
* [`send(...)`](#send)
* [`onOpen(...)`](#onopen)
* [`onMessage(...)`](#onmessage)
* [`onClose(...)`](#onclose)
* [`onError(...)`](#onerror)
* [Interfaces](#interfaces)
* [Type Aliases](#type-aliases)

### connect(...)

```typescript
connect(options
:
ConnectionOptions
) =>
Promise
```

Initiate a WebSocket connection.

| Param | Type | Description |
|---------------|-----------------------------------------------------------------|---------------------------------|
| **`options`** | ConnectionOptions | The options for the connection. |

**Since:** 0.0.1

--------------------

### close(...)

```typescript
close(options ? : CloseOptions | undefined)
=>
Promise
```

Close the connection.

| Param | Type |
|---------------|-------------------------------------------------------|
| **`options`** | CloseOptions |

--------------------

### send(...)

```typescript
send(options
:
SendMessageOptions
) =>
Promise
```

Send a message.

| Param | Type | Description |
|---------------|-------------------------------------------------------------------|------------------------------|
| **`options`** | SendMessageOptions | The options for the message. |

**Since:** 0.0.1

--------------------

### onOpen(...)

```typescript
onOpen(options
:
OnOpenOptions, callback
:
OnOpenCallback
) =>
Promise
```

Register a callback to be invoked when the connection is opened.

| Param | Type | Description |
|----------------|-----------------------------------------------------------|--------------------------------------|
| **`options`** | OnOpenOptions | The options for the connection info. |
| **`callback`** | OnOpenCallback | The callback that will be invoked. |

**Since:** 0.0.3

--------------------

### onMessage(...)

```typescript
onMessage(options
:
OnMessageOptions, callback
:
OnMessageCallback
) =>
Promise
```

Register a callback to be invoked when a message is received.

| Param | Type | Description |
|----------------|-----------------------------------------------------------------|------------------------------------|
| **`options`** | OnMessageOptions | The options for the message info. |
| **`callback`** | OnMessageCallback | The callback that will be invoked. |

**Since:** 0.0.3

--------------------

### onClose(...)

```typescript
onClose(options
:
OnCloseOptions, callback
:
OnCloseCallback
) =>
Promise
```

Register a callback to be invoked when the connection is closed.

| Param | Type | Description |
|----------------|-------------------------------------------------------------|--------------------------------------|
| **`options`** | OnCloseOptions | The options for the connection info. |
| **`callback`** | OnCloseCallback | The callback that will be invoked. |

**Since:** 0.0.3

--------------------

### onError(...)

```typescript
onError(options
:
OnErrorOptions, callback
:
OnErrorCallback
) =>
Promise
```

Register a callback to be invoked when an error occurs.

| Param | Type | Description |
|----------------|-------------------------------------------------------------|------------------------------------|
| **`options`** | OnErrorOptions | The options for the error info. |
| **`callback`** | OnErrorCallback | The callback that will be invoked. |

**Since:** 0.0.3

--------------------

### Interfaces

#### ConnectionOptions

| Prop | Type | Description | Since |
|-----------|---------------------|---------------------------------------------------------------------------------------------------------|-------|
| **`url`** | string | The URL to which to connect; this should be the URL to which the WebSocket server will respond. | 0.0.1 |
| **`id`** | string | The ID uniquely identifies a connection; no input is required, if you do not need multiple connections. | 0.0.1 |

#### CloseOptions

| Prop | Type | Description |
|--------------|---------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **`id`** | string | The ID uniquely identifies a connection; no input is required, if you do not need multiple connections. |
| **`code`** | number | An integer WebSocket connection close code value indicating a reason for closure. Status code as defined by [Section 7.4 of RFC 6455](http://tools.ietf.org/html/rfc6455#section-7.4). |
| **`reason`** | string | A string explaining the reason for the connection close. |

#### SendMessageOptions

| Prop | Type | Description | Since |
|------------|---------------------|---------------------------------------------------------------------------------------------------------|-------|
| **`data`** | string | The data to send to the server. | 0.0.1 |
| **`id`** | string | The ID uniquely identifies a connection; no input is required, if you do not need multiple connections. | 0.0.1 |

#### OnOpenOptions

| Prop | Type | Description | Since |
|----------|---------------------|---------------------------------------------------------------------------------------------------------|-------|
| **`id`** | string | The ID uniquely identifies a connection; no input is required, if you do not need multiple connections. | 0.0.1 |

#### OnOpenData

| Prop | Type | Description | Since |
|----------|---------------------|---------------------------------------------------------------------------------------------------------|-------|
| **`id`** | string | The ID uniquely identifies a connection; no input is required, if you do not need multiple connections. | 0.0.1 |

#### OnMessageOptions

| Prop | Type | Description | Since |
|----------|---------------------|---------------------------------------------------------------------------------------------------------|-------|
| **`id`** | string | The ID uniquely identifies a connection; no input is required, if you do not need multiple connections. | 0.0.1 |

#### OnMessageData

| Prop | Type | Description | Since |
|------------|---------------------|---------------------------------------------------------------------------------------------------------|-------|
| **`id`** | string | The ID uniquely identifies a connection; no input is required, if you do not need multiple connections. | 0.0.1 |
| **`data`** | string | The data sent by the message emitter. | 0.0.1 |

#### OnCloseOptions

| Prop | Type | Description | Since |
|----------|---------------------|---------------------------------------------------------------------------------------------------------|-------|
| **`id`** | string | The ID uniquely identifies a connection; no input is required, if you do not need multiple connections. | 0.0.1 |

#### OnCloseData

| Prop | Type | Description | Since |
|--------------|---------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------|
| **`id`** | string | The ID uniquely identifies a connection; no input is required, if you do not need multiple connections. | 0.0.1 |
| **`code`** | number | An integer WebSocket connection close code value indicating a reason for closure. Status code as defined by [Section 7.4 of RFC 6455](http://tools.ietf.org/html/rfc6455#section-7.4). | 0.0.1 |
| **`reason`** | string | A string explaining the reason for the connection close. | 0.0.1 |

#### OnErrorOptions

| Prop | Type | Description | Since |
|----------|---------------------|---------------------------------------------------------------------------------------------------------|-------|
| **`id`** | string | The ID uniquely identifies a connection; no input is required, if you do not need multiple connections. | 0.0.1 |

#### OnErrorData

| Prop | Type | Description | Since |
|-------------|---------------------|---------------------------------------------------------------------------------------------------------|-------|
| **`id`** | string | The ID uniquely identifies a connection; no input is required, if you do not need multiple connections. | 0.0.1 |
| **`error`** | string | The error message. | 0.0.1 |

### Type Aliases

#### OnOpenCallback

(message: OnOpenData | null, err?: any): void

#### OnMessageCallback

(message: OnMessageData | null, err?: any): void

#### OnCloseCallback

(message: OnCloseData | null, err?: any): void

#### OnErrorCallback

(message: OnErrorData | null, err?: any): void