-
-
Notifications
You must be signed in to change notification settings - Fork 41
REST and Socket connections
ADAMANT Node provides RESTful API to process clients' requests. REST requests are reliable and fit for most of tasks.
In some cases client needs to get new transactions instantly, allowing users superior performance. It is most important for messaging applications to provide smooth experience for chatting. This way nodes provide socket connections.
To enable node for sockets, refer to wsClient
section of config.json
:
"wsClient": {
"portWS": 36668,
"enabled": true
}
To check if node offers socket connections, request status
:
https://endless.adamant.im/api/node/status
Clients are able to listen socket to get notified about all of transactions, including unconfirmed yet. New unconfirmed transactions has no block_timestamp
. Most of transactions fetched by socket are new and unconfirmed as they just appeared in the ADAMANT network, and not stored in blockchain yet. Important to understand this fact to process transfer (amount
> 0) transaction correctly, check if transaction confirmed in the network, and confirmation count. Unconfirmed transaction can never become confirmed, it may be rejected by nodes' consensus.
Nodes emit transactions of all types with newTrans
event, and client app should filter them according to its needs. For socket connections, nodes store pool of transactions for 60 seconds.
Socket should be not the only connection, but additional to REST requests. This is because socket connection can be lost for some time, and fetching transactions by REST from time to time makes an application much more reliable. Still socket allows to send REST requests less often. When a new transaction received by socket, it should be checked with REST request for its status.
To listen to new transactions by socket, subscribe to node's socket events. Hare is an example:
connect (socketAddress) {
this.connection = io(`wss://${socketAddress}`, { reconnection: false, timeout: 5000 })
this.connection.on('connect', () => {
this.currentSocketAddress = socketAddress
this.connection.emit('msg', this.adamantAddress + ' connected!')
this.connection.emit('address', this.adamantAddress)
})
this.connection.on('disconnect', reason => {
if (reason === 'ping timeout' || reason === 'io server disconnect') {
console.warn('[Socket] Disconnected. Reason:', reason)
}
})
this.connection.on('connect_error', (err) => {
console.warn('[Socket] connect_error', err)
})
}