mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-21 04:05:49 +00:00
248 lines
11 KiB
Markdown
248 lines
11 KiB
Markdown
# Monitor Incoming Payments with WebSocket
|
||
|
||
This tutorial shows how to monitor for incoming payments using the [WebSocket `rippled` API](rippled-api.html). Since all XRP Ledger transactions are public, anyone can monitor incoming payments to any address.
|
||
|
||
WebSocket follows a model where the client and server establish one connection, then send messages both ways through the same connection, which remains open until explicitly closed (or until the connection fails). This is in contrast to the HTTP-based API model (including JSON-RPC and RESTful APIs), where the client opens and closes a new connection for each request[¹](#footnote-1)<a id="from-footnote-1"></a>.
|
||
|
||
**Tip:** The examples in this page use JavaScript so that the examples can run natively in a web browser. If you are developing in JavaScript, you can also use the [RippleAPI library for JavaScript](https://developers.ripple.com/rippleapi-reference.html) to simplify some tasks. This tutorial shows how to monitor for transactions _without_ using RippleAPI so that you can translate the steps to other programming languages that don't have RippleAPI.
|
||
|
||
## Prerequisites
|
||
|
||
- The examples in this page use JavaScript and the WebSocket protocol, which are available in all major modern browsers. If you have some JavaScript knowledge and expertise in another programming language with a WebSocket client, you can follow along while adapting the instructions to the language of your choice.
|
||
- You need a stable internet connection and access to a `rippled` server. The embedded examples connect to Ripple's pool of public servers. If you [run your own `rippled` server](install-rippled.html), you can also connect to that server locally.
|
||
|
||
<script type="application/javascript">
|
||
// Helper stuff for this interactive tutorial
|
||
|
||
function writeToConsole(console_selector, message) {
|
||
let write_msg = "<div class='console-entry'>" + message + "</div>"
|
||
$(console_selector).find(".placeholder").remove()
|
||
$(console_selector).append(write_msg)
|
||
// TODO: JSON pretty-printing, maybe w/ multiple input args?
|
||
}
|
||
|
||
</script>
|
||
|
||
{% set n = cycler(* range(1,99)) %}
|
||
|
||
## {{n.next()}}. Connect to the XRP Ledger
|
||
|
||
The first step of monitoring for incoming payments is to connect to the XRP Ledger, specifically a `rippled` server.
|
||
|
||
The following JavaScript code connects to one of Ripple's public server clusters. It then logs a message to the console, sends a requesting using the [ping method][] and sets up a handler to log to the console again when it receives any message from the server side.
|
||
|
||
```js
|
||
const socket = new WebSocket('wss://s.altnet.rippletest.net:51233')
|
||
socket.addEventListener('open', (event) => {
|
||
// This callback runs when the connection is open
|
||
console.log("Connected!")
|
||
const command = {
|
||
"id": "on_open_ping_1",
|
||
"command": "ping"
|
||
}
|
||
socket.send(JSON.stringify(command))
|
||
})
|
||
socket.addEventListener('message', (event) => {
|
||
console.log('Got message from server:', event.data)
|
||
})
|
||
```
|
||
|
||
The above example opens a secure connection (`wss://`) to one of Ripple's public Test Net API servers. To connect to a locally-running `rippled` server with the default configuration instead, open an _unsecured_ connection (`ws://`) on port **6006** locally, using the following first line:
|
||
|
||
```js
|
||
const socket = new WebSocket('ws://localhost:6006')
|
||
```
|
||
|
||
**Tip:** By default, connecting to a local `rippled` server gives you access to the full set of [admin methods](admin-rippled-methods.html) and admin-only data in some responses such as [server_info][server_info method], in addition to the [public methods](public-rippled-methods.html) that are available when you connect to a public servers over the internet.
|
||
|
||
Example:
|
||
|
||
{{ start_step("Connect") }}
|
||
<button id="connect-button" class="btn btn-primary">Connect</button>
|
||
<strong>Connection status:</strong>
|
||
<span id="connection-status">Not connected</span>
|
||
<div id='loader-{{n.current}}' style="display: none;"><img class='throbber' src="assets/img/rippleThrobber.png"></div>
|
||
<h5>Console:</h5>
|
||
<div class="ws-console" id="monitor-console-connect"><span class="placeholder">(Log is empty)</span></div>
|
||
{{ end_step() }}
|
||
|
||
<script type="application/javascript">
|
||
let socket;
|
||
$("#connect-button").click((event) => {
|
||
socket = new WebSocket('wss://s.altnet.rippletest.net:51233')
|
||
socket.addEventListener('open', (event) => {
|
||
// This callback runs when the connection is open
|
||
writeToConsole("#monitor-console-connect", "Connected!")
|
||
const command = {
|
||
"id": "on_open_ping_1",
|
||
"command": "ping"
|
||
}
|
||
socket.send(JSON.stringify(command))
|
||
|
||
// TODO: mark current breadcrumb as done, enable next step breadcrumb
|
||
$("#enable_dispatcher").prop("disabled",false)
|
||
})
|
||
socket.addEventListener('message', (event) => {
|
||
writeToConsole("#monitor-console-connect", "Got message from server: "+JSON.stringify(event.data))
|
||
})
|
||
})
|
||
</script>
|
||
|
||
|
||
## {{n.next()}}. Dispatch Incoming Messages to Handlers
|
||
|
||
Since WebSocket connections can have several messages going each way and there is not a strict 1:1 correlation between requests and responses, you need to identify what to do with each incoming message. A good model for coding this is to set up a "dispatcher" function that reads incoming messages and relays each message to the correct code path for handling it. To help dispatch messages appropriately, the `rippled` server provides a `type` field on every WebSocket message:
|
||
|
||
- For any message that is a direct response to a request from the client side, the `type` is the string `response`. In this case, the server also provides the following:
|
||
|
||
- An `id` field that matches the `id` provided in the request this is a response for. (This is important because responses may arrive out of order.)
|
||
|
||
- A `status` field that indicates whether the API successfully processed your request. The string value `success` indicates [a successful response](response-formatting.html). The string value `error` indicates [an error](error-formatting.html).
|
||
|
||
**Warning:** When submitting transactions, a `status` of `success` at the top level of the WebSocket message does not mean that the transaction itself succeeded. It only indicates that the server understood your request. For looking up a transaction's actual outcome, see [Look Up Transaction Results](look-up-transaction-results.html).
|
||
|
||
- For follow-up messages from [subscriptions](subscribe.html), the `type` indicates the type of follow-up message it is, such as the notification of an new transaction, ledger, or validation; or a follow-up to an ongoing [pathfinding request](path_find.html). Your client only receives these messages if it subscribes to them.
|
||
|
||
**Tip:** The [RippleAPI library for JavaScript](rippleapi-reference.html) handles this step by default. All asynchronous API requests use Promises to provide the response, and you can listen to streams using the [`.on(event, callback)` method](rippleapi-reference.html#listening-to-streams).
|
||
|
||
The following JavaScript code defines a helper function to make API requests into convenient asynchronous [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises), and sets up an interface to map other types of messages to global handlers:
|
||
|
||
```js
|
||
const AWAITING = {}
|
||
const handleResponse = function(data) {
|
||
if (!data.hasOwnProperty("id")) {
|
||
console.error("Got response event without ID:", data)
|
||
return
|
||
}
|
||
if (AWAITING.hasOwnProperty(data.id)) {
|
||
AWAITING[data.id].resolve(data)
|
||
} else {
|
||
console.error("Response to un-awaited request w/ ID "+data.id)
|
||
}
|
||
}
|
||
|
||
let autoid_n = 0
|
||
function api_request(options) {
|
||
if (!options.hasOwnProperty("id")) {
|
||
options.id = "autoid_" + (autoid_n++)
|
||
}
|
||
|
||
let resolveHolder;
|
||
AWAITING[options.id] = new Promise((resolve, reject) => {
|
||
// Save the resolve func to be called by the handleResponse function later
|
||
this.resolve = resolve
|
||
try {
|
||
// Use the socket opened in the previous example...
|
||
socket.send(JSON.stringify(options))
|
||
} catch {
|
||
reject()
|
||
}
|
||
})
|
||
AWAITING[options.id].resolve = resolveHolder;
|
||
return AWAITING[options.id]
|
||
}
|
||
|
||
const WS_HANDLERS = {
|
||
"response": handleResponse
|
||
// Fill this out with your handlers in the following format:
|
||
// "type": function(event) { /* handle event of this type */ }
|
||
}
|
||
socket.addEventListener('message', (event) => {
|
||
const parsed_data = JSON.parse(event.data)
|
||
if (WS_HANDLERS.hasOwnProperty(parsed_data.type)) {
|
||
// Call the mapped handler
|
||
WS_HANDLERS[parsed_data.type](parsed_data)
|
||
} else {
|
||
console.log("Unhandled message from server", event)
|
||
}
|
||
})
|
||
|
||
// Demonstrate api_request functionality
|
||
async function pingpong() {
|
||
console.log("Ping...")
|
||
const response = await api_request({command: "ping"})
|
||
console.log("Pong!", response)
|
||
}
|
||
pingpong()
|
||
```
|
||
|
||
{{ start_step("Dispatch Messages") }}
|
||
<button id="enable_dispatcher" class="btn btn-primary" disabled="disabled">Enable Dispatcher</button>
|
||
<button id="dispatch_ping" class="btn btn-primary" disabled="disabled">Ping!</button>
|
||
<h5>Responses</h5>
|
||
<div class="ws-console" id="monitor-console-ping"><span class="placeholder">(Log is empty)</span></div>
|
||
{{ end_step() }}
|
||
|
||
<script type="application/javascript">
|
||
const AWAITING = {}
|
||
const handleResponse = function(data) {
|
||
if (!data.hasOwnProperty("id")) {
|
||
writeToConsole("#monitor-console-ping", "Got response event without ID:", data)
|
||
return
|
||
}
|
||
if (AWAITING.hasOwnProperty(data.id)) {
|
||
AWAITING[data.id].resolve(data)
|
||
} else {
|
||
writeToConsole("#monitor-console-ping", "Response to un-awaited request w/ ID "+data.id)
|
||
}
|
||
}
|
||
|
||
let autoid_n = 0
|
||
function api_request(options) {
|
||
if (!options.hasOwnProperty("id")) {
|
||
options.id = "autoid_" + (autoid_n++)
|
||
}
|
||
let resolveFunc;
|
||
AWAITING[options.id] = new Promise((resolve, reject) => {
|
||
// Save the resolve func to be called by the handleResponse function later
|
||
resolveFunc = resolve
|
||
try {
|
||
// Use the socket opened in the previous example...
|
||
socket.send(JSON.stringify(options))
|
||
} catch {
|
||
reject()
|
||
}
|
||
})
|
||
AWAITING[options.id].resolve = resolveFunc
|
||
return AWAITING[options.id]
|
||
}
|
||
|
||
const WS_HANDLERS = {
|
||
"response": handleResponse
|
||
}
|
||
$("#enable_dispatcher").click((clickEvent) => {
|
||
socket.addEventListener('message', (event) => {
|
||
const parsed_data = JSON.parse(event.data)
|
||
if (WS_HANDLERS.hasOwnProperty(parsed_data.type)) {
|
||
// Call the mapped handler
|
||
WS_HANDLERS[parsed_data.type](parsed_data)
|
||
} else {
|
||
writeToConsole("#monitor-console-ping", "Unhandled message from server: "+event)
|
||
}
|
||
})
|
||
$("#dispatch_ping").prop("disabled", false)
|
||
})
|
||
|
||
async function pingpong() {
|
||
const response = await api_request({command: "ping"})
|
||
writeToConsole("#monitor-console-ping", "Pong! "+JSON.stringify(response))
|
||
}
|
||
|
||
$("#dispatch_ping").click((event) => {
|
||
pingpong()
|
||
})
|
||
</script>
|
||
|
||
|
||
## Footnotes
|
||
|
||
[1.](#from-footnote-1) <a id="footnote-1"></a> In practice, when calling an HTTP-based API multiple times, the client and server may reuse the same connection for several requests and responses. This practice is called [HTTP persistent connection, or keep-alive](https://en.wikipedia.org/wiki/HTTP_persistent_connection). From a development standpoint, the code to use an HTTP-based API is the same regardless of whether the underlying connection is new or reused.
|
||
|
||
|
||
|
||
|
||
<!--{# common link defs #}-->
|
||
{% include '_snippets/rippled-api-links.md' %}
|
||
{% include '_snippets/tx-type-links.md' %}
|
||
{% include '_snippets/rippled_versions.md' %}
|