Simple WebSockets client and server for the Capacitor framework. It's only a really basic implementation with some caveats. If you find any bugs or have improvements create an issue or pull request.
npm install capacitor-websockets
npx cap sync
startServer(...)
startClient(...)
stop()
sendMessage(...)
addListener('onOpen', ...)
addListener('onClose', ...)
addListener('onMessage', ...)
addListener('onError', ...)
- Interfaces
startServer(options?: StartOptions | undefined) => Promise<{ port: number; }>
Param | Type |
---|---|
options |
StartOptions |
Returns: Promise<{ port: number; }>
startClient(options?: ClientOptions | undefined) => Promise<{ url: string; }>
Param | Type |
---|---|
options |
ClientOptions |
Returns: Promise<{ url: string; }>
stop() => Promise<void>
sendMessage(options?: MessageData | undefined) => Promise<void>
Param | Type |
---|---|
options |
MessageData |
addListener(eventName: 'onOpen', listenerFunc: (connection: SocketData) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
'onOpen' |
listenerFunc |
(connection: SocketData) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
addListener(eventName: 'onClose', listenerFunc: (data: CloseData) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
'onClose' |
listenerFunc |
(data: CloseData) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
addListener(eventName: 'onMessage', listenerFunc: (data: MessageData) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
'onMessage' |
listenerFunc |
(data: MessageData) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
addListener(eventName: 'onError', listenerFunc: (data: CloseData) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
'onError' |
listenerFunc |
(data: CloseData) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
Prop | Type |
---|---|
port |
number |
Prop | Type |
---|---|
url |
string |
Prop | Type |
---|---|
connection |
SocketData |
message |
string |
Prop | Type |
---|---|
ip |
string |
host |
number |
Prop | Type |
---|---|
remove |
() => Promise<void> |
Prop | Type |
---|---|
connection |
SocketData |
error |
string |