Basic, cross-platform native websocket implementation for Capacitor/Ionic.
- iOS/Swift implmentation uses StarScream
- Android/Java uses NV/NeoVisionaries Websocket Client
- Web implementation uses web native websockets
npm install @miaz/capacitor-websocket
npx cap sync
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.
type EventSourceNames = "chat" | "live";
addListener<EventSourceNames>("chat:message", ...);
addListener<EventSourceNames>("live:message", ...);
connect(...)
disconnect(...)
send(...)
applyListeners(...)
build(...)
addListener(
${T}:message, ...)
addListener(
${T}:connected, ...)
addListener(
${T}:disconnected, ...)
addListener(
${T}:statechange, ...)
addListener(
${T}:closeframe, ...)
addListener(
${T}:connecterror, ...)
addListener(
${T}:error, ...)
addListener(
${T}:messageerror, ...)
addListener(
${T}:senderror, ...)
addListener(
${T}:textmessageerror, ...)
addListener(
${T}:textmessage, ...)
- Interfaces
- Type Aliases
connect(options: ConnectOptions) => Promise<void>
Param | Type |
---|---|
options |
OptionsBase |
disconnect(options: DisconnectOptions) => Promise<void>
Param | Type |
---|---|
options |
OptionsBase |
send(options: SendOptions) => Promise<void>
Param | Type |
---|---|
options |
SendOptions |
applyListeners(options: ApplyListenersOptions) => Promise<void>
Param | Type |
---|---|
options |
OptionsBase |
build(options: BuildOptions) => Promise<void>
Param | Type |
---|---|
options |
BuildOptions |
addListener<T extends string>(eventName: `${T}:message`, listenerFunc: (event: MessageEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
|
listenerFunc |
(event: MessageEvent) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
addListener<T extends string>(eventName: `${T}:connected`, listenerFunc: (event: ConnectedEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
|
listenerFunc |
(event: ConnectedEvent) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
addListener<T extends string>(eventName: `${T}:disconnected`, listenerFunc: (event: DisconnectedEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
|
listenerFunc |
(event: DisconnectedEvent) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
addListener<T extends string>(eventName: `${T}:statechange`, listenerFunc: (event: StateChangedEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
|
listenerFunc |
(event: StateChangedEvent) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
addListener<T extends string>(eventName: `${T}:closeframe`, listenerFunc: (event: CloseFrameEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
|
listenerFunc |
(event: CloseFrameEvent) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
addListener<T extends string>(eventName: `${T}:connecterror`, listenerFunc: (event: ConnectErrorEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
|
listenerFunc |
(event: ConnectErrorEvent) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
addListener<T extends string>(eventName: `${T}:error`, listenerFunc: (event: ErrorEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
|
listenerFunc |
(event: ErrorEvent) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
addListener<T extends string>(eventName: `${T}:messageerror`, listenerFunc: (event: MessageErrorEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
|
listenerFunc |
(event: MessageErrorEvent) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
addListener<T extends string>(eventName: `${T}:senderror`, listenerFunc: (event: SendErrorEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
|
listenerFunc |
(event: SendErrorEvent) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
addListener<T extends string>(eventName: `${T}:textmessageerror`, listenerFunc: (event: TextMessageErrorEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
|
listenerFunc |
(event: TextMessageErrorEvent) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
addListener<T extends string>(eventName: `${T}:textmessage`, listenerFunc: (event: TextMessageEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
|
listenerFunc |
(event: TextMessageEvent) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
Prop | Type |
---|---|
remove |
() => Promise<void> |
{ name: string, }
OptionsBase & { data: any }
OptionsBase & { url: string, headers?: { [headerKey: string]: string, } }
{ data: string }
{ headers: { [x: string]: string[] } }
{ closedByServer: boolean, serverCloseFrame: string, clientCloseFrame: string }
{ state: string }
{ frame: string }
{ exception: string }
{ cause: string }
{ cause: string, frame: string }
{ cause: string, frame: string }
{ data: string, cause: string }
{ data: string }