Skip to content

Commit

Permalink
Add readme for websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikJannsen committed Jan 13, 2025
1 parent 6407553 commit eae85ab
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Websocket overview

We use websockets for getting data pushed from the back-end.

## Rest Api proxy

As in case of Tor we want to avoid establishing unnecessary new connections due the delay and risk that a connection attempt fails, we
use the existing websocket connection to tunnel the Rest API requests to the backend server which unpacks it and does a local Rest Api
request and returns the result in the websocket connection.
The request blocks until the response is provided.

Note: For POST and PATCH there is a unresolved bug on the backend, so we use the Rest Api endpoints directly for now.

## Topics

Topics are used to subscribe for certain types of events.

## Subscription

To subscribe to some data stream we use a subscription ID and topic. Optional a parameter can be added.
The response to the `SubscriptionRequest` will contain an optional payload and optional errorMessage.
The payload is a json string and will be deserialized by the actual subscriber.
The type of the payload is defined by the Subscription as well as provided in the Topic as a `kotlin.reflect.KType`.

## Events

When a data update on the back-end is propagated to the client, we receive a `WebSocketEvent`.
This contains the payload, the `ModificationType` and a sequence number. The sequence number is to ensure order of events.
`ModificationType` gives information if data was added, removed or replaced.
We use a list for the payloads to support multiple changes being batched in one event.






Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class WebSocketApiClient(
val apiPath = "/api/v1/"
var apiUrl = "http://$host:$port$apiPath"

// POST request still not working, but issue is likely on the bisq2 side.
// POST and PATCH request are not working yes on the backend.
// So we use httpClient instead.
val useHttpClientForPost = true
val useHttpClient = true

suspend inline fun <reified T> get(path: String): Result<T> {
return request<T>("GET", path)
Expand All @@ -55,7 +55,7 @@ class WebSocketApiClient(
}

suspend inline fun <reified T, reified R> patch(path: String, requestBody: R): Result<T> {
if (useHttpClientForPost) {
if (useHttpClient) {
try {
val response: HttpResponse = httpClient.patch(apiUrl + path) {
contentType(ContentType.Application.Json)
Expand All @@ -76,7 +76,7 @@ class WebSocketApiClient(
}

suspend inline fun <reified T, reified R> post(path: String, requestBody: R): Result<T> {
if (useHttpClientForPost) {
if (useHttpClient) {
try {
val response: HttpResponse = httpClient.post(apiUrl + path) {
contentType(ContentType.Application.Json)
Expand Down

0 comments on commit eae85ab

Please sign in to comment.