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: about 1 year ago
JSON representation
Native WebSocket plugin for Capacitor/Ionic
- Host: GitHub
- URL: https://github.com/mia-z/capacitor-websocket
- Owner: mia-z
- Created: 2023-02-07T14:05:06.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-09-08T15:58:04.000Z (almost 3 years ago)
- Last Synced: 2025-03-29T06:51:17.004Z (about 1 year ago)
- Topics: capacitor, ionic, typescript, websocket
- Language: Java
- Homepage:
- Size: 63.3 MB
- Stars: 18
- Watchers: 2
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
Awesome Lists containing this project
- awesome-capacitor - WebSocket - Plugin providing native WebSocket implementations for each platform. (Community plugins)
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
{ name: string, }
#### DisconnectOptions
#### SendOptions
OptionsBase & { data: any }
#### ApplyListenersOptions
#### 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 }