Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/mia-z/capacitor-websocket

Native WebSocket plugin for Capacitor/Ionic
https://github.com/mia-z/capacitor-websocket

capacitor ionic typescript websocket

Last synced: 2 months ago
JSON representation

Native WebSocket plugin for Capacitor/Ionic

Awesome Lists containing this project

README

        

# websocket

Basic, cross-platform native websocket implementation for Capacitor/Ionic.

- iOS/Swift implmentation uses [StarScream](/https://github.com/daltoniam/Starscream)
- Android/Java uses [NV/NeoVisionaries Websocket Client](/https://github.com/TakahikoKawasaki/nv-websocket-client)
- Web implementation uses web native websockets

## Install

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

## Usage

Rather than overriding Capacitor's addListener and notifyListener under the hood, create specific event names to the source they're coming from, separated with a colon and then the event name itself.

```typescript
type EventSourceNames = "chat" | "live";

addListener("chat:message", ...);
addListener("live:message", ...);
```

## API

* [`connect(...)`](#connect)
* [`disconnect(...)`](#disconnect)
* [`send(...)`](#send)
* [`applyListeners(...)`](#applylisteners)
* [`build(...)`](#build)
* [`addListener(`${T}:message`, ...)`](#addlistenertmessage)
* [`addListener(`${T}:connected`, ...)`](#addlistenertconnected)
* [`addListener(`${T}:disconnected`, ...)`](#addlistenertdisconnected)
* [`addListener(`${T}:statechange`, ...)`](#addlistenertstatechange)
* [`addListener(`${T}:closeframe`, ...)`](#addlistenertcloseframe)
* [`addListener(`${T}:connecterror`, ...)`](#addlistenertconnecterror)
* [`addListener(`${T}:error`, ...)`](#addlistenerterror)
* [`addListener(`${T}:messageerror`, ...)`](#addlistenertmessageerror)
* [`addListener(`${T}:senderror`, ...)`](#addlistenertsenderror)
* [`addListener(`${T}:textmessageerror`, ...)`](#addlistenerttextmessageerror)
* [`addListener(`${T}:textmessage`, ...)`](#addlistenerttextmessage)
* [Interfaces](#interfaces)
* [Type Aliases](#type-aliases)

### connect(...)

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

| Param | Type |
| ------------- | --------------------------------------------------- |
| **`options`** | OptionsBase |

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

### disconnect(...)

```typescript
disconnect(options: DisconnectOptions) => Promise
```

| Param | Type |
| ------------- | --------------------------------------------------- |
| **`options`** | OptionsBase |

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

### send(...)

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

| Param | Type |
| ------------- | --------------------------------------------------- |
| **`options`** | SendOptions |

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

### applyListeners(...)

```typescript
applyListeners(options: ApplyListenersOptions) => Promise
```

| Param | Type |
| ------------- | --------------------------------------------------- |
| **`options`** | OptionsBase |

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

### build(...)

```typescript
build(options: BuildOptions) => Promise
```

| Param | Type |
| ------------- | ----------------------------------------------------- |
| **`options`** | BuildOptions |

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

### addListener(`${T}:message`, ...)

```typescript
addListener(eventName: `${T}:message`, listenerFunc: (event: MessageEvent) => void) => Promise & PluginListenerHandle
```

| Param | Type |
| ------------------ | ------------------------------------------------------------------------- |
| **`eventName`** | `${T}:message` |
| **`listenerFunc`** | (event: MessageEvent) => void |

**Returns:** Promise<PluginListenerHandle> & PluginListenerHandle

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

### addListener(`${T}:connected`, ...)

```typescript
addListener(eventName: `${T}:connected`, listenerFunc: (event: ConnectedEvent) => void) => Promise & PluginListenerHandle
```

| Param | Type |
| ------------------ | ----------------------------------------------------------------------------- |
| **`eventName`** | `${T}:connected` |
| **`listenerFunc`** | (event: ConnectedEvent) => void |

**Returns:** Promise<PluginListenerHandle> & PluginListenerHandle

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

### addListener(`${T}:disconnected`, ...)

```typescript
addListener(eventName: `${T}:disconnected`, listenerFunc: (event: DisconnectedEvent) => void) => Promise & PluginListenerHandle
```

| Param | Type |
| ------------------ | ----------------------------------------------------------------------------------- |
| **`eventName`** | `${T}:disconnected` |
| **`listenerFunc`** | (event: DisconnectedEvent) => void |

**Returns:** Promise<PluginListenerHandle> & PluginListenerHandle

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

### addListener(`${T}:statechange`, ...)

```typescript
addListener(eventName: `${T}:statechange`, listenerFunc: (event: StateChangedEvent) => void) => Promise & PluginListenerHandle
```

| Param | Type |
| ------------------ | ----------------------------------------------------------------------------------- |
| **`eventName`** | `${T}:statechange` |
| **`listenerFunc`** | (event: StateChangedEvent) => void |

**Returns:** Promise<PluginListenerHandle> & PluginListenerHandle

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

### addListener(`${T}:closeframe`, ...)

```typescript
addListener(eventName: `${T}:closeframe`, listenerFunc: (event: CloseFrameEvent) => void) => Promise & PluginListenerHandle
```

| Param | Type |
| ------------------ | ------------------------------------------------------------------------------- |
| **`eventName`** | `${T}:closeframe` |
| **`listenerFunc`** | (event: CloseFrameEvent) => void |

**Returns:** Promise<PluginListenerHandle> & PluginListenerHandle

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

### addListener(`${T}:connecterror`, ...)

```typescript
addListener(eventName: `${T}:connecterror`, listenerFunc: (event: ConnectErrorEvent) => void) => Promise & PluginListenerHandle
```

| Param | Type |
| ------------------ | ----------------------------------------------------------------------------------- |
| **`eventName`** | `${T}:connecterror` |
| **`listenerFunc`** | (event: ConnectErrorEvent) => void |

**Returns:** Promise<PluginListenerHandle> & PluginListenerHandle

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

### addListener(`${T}:error`, ...)

```typescript
addListener(eventName: `${T}:error`, listenerFunc: (event: ErrorEvent) => void) => Promise & PluginListenerHandle
```

| Param | Type |
| ------------------ | --------------------------------------------------------------------- |
| **`eventName`** | `${T}:error` |
| **`listenerFunc`** | (event: ErrorEvent) => void |

**Returns:** Promise<PluginListenerHandle> & PluginListenerHandle

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

### addListener(`${T}:messageerror`, ...)

```typescript
addListener(eventName: `${T}:messageerror`, listenerFunc: (event: MessageErrorEvent) => void) => Promise & PluginListenerHandle
```

| Param | Type |
| ------------------ | ----------------------------------------------------------------------------------- |
| **`eventName`** | `${T}:messageerror` |
| **`listenerFunc`** | (event: MessageErrorEvent) => void |

**Returns:** Promise<PluginListenerHandle> & PluginListenerHandle

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

### addListener(`${T}:senderror`, ...)

```typescript
addListener(eventName: `${T}:senderror`, listenerFunc: (event: SendErrorEvent) => void) => Promise & PluginListenerHandle
```

| Param | Type |
| ------------------ | ----------------------------------------------------------------------------- |
| **`eventName`** | `${T}:senderror` |
| **`listenerFunc`** | (event: SendErrorEvent) => void |

**Returns:** Promise<PluginListenerHandle> & PluginListenerHandle

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

### addListener(`${T}:textmessageerror`, ...)

```typescript
addListener(eventName: `${T}:textmessageerror`, listenerFunc: (event: TextMessageErrorEvent) => void) => Promise & PluginListenerHandle
```

| Param | Type |
| ------------------ | ------------------------------------------------------------------------------------------- |
| **`eventName`** | `${T}:textmessageerror` |
| **`listenerFunc`** | (event: TextMessageErrorEvent) => void |

**Returns:** Promise<PluginListenerHandle> & PluginListenerHandle

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

### addListener(`${T}:textmessage`, ...)

```typescript
addListener(eventName: `${T}:textmessage`, listenerFunc: (event: TextMessageEvent) => void) => Promise & PluginListenerHandle
```

| Param | Type |
| ------------------ | --------------------------------------------------------------------------------- |
| **`eventName`** | `${T}:textmessage` |
| **`listenerFunc`** | (event: TextMessageEvent) => void |

**Returns:** Promise<PluginListenerHandle> & PluginListenerHandle

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

### Interfaces

#### PluginListenerHandle

| Prop | Type |
| ------------ | ----------------------------------------- |
| **`remove`** | () => Promise<void> |

### Type Aliases

#### ConnectOptions

OptionsBase

#### OptionsBase

{ name: string, }

#### DisconnectOptions

OptionsBase

#### SendOptions

OptionsBase & { data: any }

#### ApplyListenersOptions

OptionsBase

#### BuildOptions

OptionsBase & { url: string, headers?: { [headerKey: string]: string, } }

#### MessageEvent

{ data: string }

#### ConnectedEvent

{ headers: { [x: string]: string[] } }

#### DisconnectedEvent

{ closedByServer: boolean, serverCloseFrame: string, clientCloseFrame: string }

#### StateChangedEvent

{ state: string }

#### CloseFrameEvent

{ frame: string }

#### ConnectErrorEvent

{ exception: string }

#### ErrorEvent

{ cause: string }

#### MessageErrorEvent

{ cause: string, frame: string }

#### SendErrorEvent

{ cause: string, frame: string }

#### TextMessageErrorEvent

{ data: string, cause: string }

#### TextMessageEvent

{ data: string }