Skip to content

mia-z/capacitor-websocket

Repository files navigation

websocket

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

Install

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.

type EventSourceNames = "chat" | "live";

addListener<EventSourceNames>("chat:message", ...);
addListener<EventSourceNames>("live:message", ...);

API

connect(...)

connect(options: ConnectOptions) => Promise<void>
Param Type
options OptionsBase

disconnect(...)

disconnect(options: DisconnectOptions) => Promise<void>
Param Type
options OptionsBase

send(...)

send(options: SendOptions) => Promise<void>
Param Type
options SendOptions

applyListeners(...)

applyListeners(options: ApplyListenersOptions) => Promise<void>
Param Type
options OptionsBase

build(...)

build(options: BuildOptions) => Promise<void>
Param Type
options BuildOptions

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

addListener<T extends string>(eventName: `${T}:message`, listenerFunc: (event: MessageEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param Type
eventName ${T}:message
listenerFunc (event: MessageEvent) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle


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

addListener<T extends string>(eventName: `${T}:connected`, listenerFunc: (event: ConnectedEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param Type
eventName ${T}:connected
listenerFunc (event: ConnectedEvent) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle


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

addListener<T extends string>(eventName: `${T}:disconnected`, listenerFunc: (event: DisconnectedEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param Type
eventName ${T}:disconnected
listenerFunc (event: DisconnectedEvent) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle


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

addListener<T extends string>(eventName: `${T}:statechange`, listenerFunc: (event: StateChangedEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param Type
eventName ${T}:statechange
listenerFunc (event: StateChangedEvent) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle


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

addListener<T extends string>(eventName: `${T}:closeframe`, listenerFunc: (event: CloseFrameEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param Type
eventName ${T}:closeframe
listenerFunc (event: CloseFrameEvent) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle


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

addListener<T extends string>(eventName: `${T}:connecterror`, listenerFunc: (event: ConnectErrorEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param Type
eventName ${T}:connecterror
listenerFunc (event: ConnectErrorEvent) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle


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

addListener<T extends string>(eventName: `${T}:error`, listenerFunc: (event: ErrorEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param Type
eventName ${T}:error
listenerFunc (event: ErrorEvent) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle


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

addListener<T extends string>(eventName: `${T}:messageerror`, listenerFunc: (event: MessageErrorEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param Type
eventName ${T}:messageerror
listenerFunc (event: MessageErrorEvent) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle


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

addListener<T extends string>(eventName: `${T}:senderror`, listenerFunc: (event: SendErrorEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param Type
eventName ${T}:senderror
listenerFunc (event: SendErrorEvent) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle


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

addListener<T extends string>(eventName: `${T}:textmessageerror`, listenerFunc: (event: TextMessageErrorEvent) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param Type
eventName ${T}:textmessageerror
listenerFunc (event: TextMessageErrorEvent) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle


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

addListener<T extends string>(eventName: `${T}:textmessage`, listenerFunc: (event: TextMessageEvent) => void) => Promise<PluginListenerHandle> & 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 }