A simple client WebSocket wrapper
This module is 100% specific and only works in combination with KissJS server. The websocket wrapper uses kiss.session to authenticate the user with the server.
Methods
# async static init(config)
Init the WebSocket connection
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
config |
object
|
|||
socketUrl |
string
|
<optional> |
optional socket url to use |
|
port |
string
|
<optional> |
optional socket non-secure port to use |
|
sslPort |
string
|
<optional> |
optional socket secure port to use |
|
reconnection |
object
|
<optional> |
optional reconnection config |
|
reconnection.enabled |
boolean
|
<optional> |
true | optional enable/disable reconnection |
reconnection.delay |
number
|
<optional> |
5000 | optional reconnection delay in ms |
reconnection.delta |
number
|
<optional> |
2 | optional reconnection delta to avoid DDOS on server reboot |
reconnection.maxAttempts |
number
|
<optional> |
10 | optional max reconnection in a row. Reset each time the connection is established. |
heartbeat |
object
|
<optional> |
optional heartbeat config |
|
heartbeat.enabled |
boolean
|
<optional> |
true | optional enable/disable heartbeat |
heartbeat.delay |
number
|
<optional> |
10000 | optional heartbeat frequency in ms |
heartbeat.timeout |
number
|
<optional> |
35000 | optional heartbeat timeout before considering the socket as closed. Should be set several time greater than heartbeat delay to be sure pings are not answered by the server, because it may be busy or may forget to send the pong response. |
onopen |
function
|
<optional> |
Hook to the onopen event |
|
onmessage |
function
|
<optional> |
Hook to the onmessage event |
|
onclose |
function
|
<optional> |
Hook to the onclose event |
Example
await kiss.websocket.init()
kiss.websocket.send("something")
// More complex case:
await kiss.websocket.init({
socketUrl: "wss://api.valr.com/ws/trade",
onopen: () => {
kiss.websocket.send({
type: "SUBSCRIBE",
subscriptions: [{
event: "AGGREGATED_ORDERBOOK_UPDATE",
pairs: ["BTCZAR"]
}]
})
},
onmessage: (message) => {
console.log("Message received: ", message)
},
onclose: () => {
console.log("Socket closed!")
}
})
# static send(jsonData)
Send a message to the server via WebSocket
Parameters:
Name | Type | Description |
---|---|---|
jsonData |
object
|
Any valid JSON |
Example
kiss.data.send({
userId: "john.doe@pickaform.com",
text: "Hello, how are you?"
})