1

Consider the following TS client code snippet below :

this.rSocketClient.connect().subscribe({
      onComplete: socket => {
        const endpoint = ‚accesscode’;
        socket.requestResponse({
          data: clientId, //the clientId is a non-empty string
          metadata: String.fromCharCode(endpoint.length) + endpoint
        }).subscribe({
            onComplete: () => console.log(’Done’),
            onError: error => {
              console.log('Connection has been closed due to:: ' + error);
            },
          });
      },
      onError: error => console.error(error),
      onSubscribe: cancel => {}
    });

I would like to know how the signature of my server accesscode endpoint should look like. In fact I tried the following solution, but it didn't workout as expected. In fact when I put a breakpoint within getAccessCode, the call gets caught but clientId cannot be resolved:

@MessageMapping("accesscode")
public Mono<String> getAccessCode(@Payload String clientId) {
  log.info("requested clientId:"+clientId);
  //Server side processing to read the corresponding 'accesscode' from the DB
  return Mono.just(DefaultPayload.create(accesscode));
}

Any help will be more than appreciated.

2 Answers2

1

Your client needs to send the routing information that spring-boot makes it decisions off. See https://domenicosibilio.medium.com/rsocket-with-spring-boot-js-zero-to-hero-ef63128f973d

Specifically look at the mime type in setting up the client, and the route which should match the @MessageMapping your your code.

  // Create an instance of a client
  client = new RSocketClient({
    serializers: {
      data: JsonSerializer,
      metadata: IdentitySerializer
    },
    setup: {
      // ms btw sending keepalive to server
      keepAlive: 60000,
      // ms timeout if no keepalive response
      lifetime: 180000,
      // format of `data`
      dataMimeType: 'application/json',
      // format of `metadata`
      metadataMimeType: 'message/x.rsocket.routing.v0',
    },
    transport: new RSocketWebSocketClient({
      url: 'ws://localhost:8080/tweetsocket'
    }),
  });

  // Open the connection
  client.connect().subscribe({
    onComplete: socket => {
      // socket provides the rsocket interactions fire/forget, request/response,
      // request/stream, etc as well as methods to close the socket.
      socket.requestStream({
        data: {
          'author': document.getElementById("author-filter").value
        },
        metadata: String.fromCharCode('tweets.by.author'.length) + 'tweets.by.author',
      }).subscribe({
        onComplete: () => console.log('complete'),
        onError: error => {
          console.log(error);
          addErrorMessage("Connection has been closed due to ", error);
        },
        onNext: payload => {
          console.log(payload.data);
          addMessage(payload.data);
        },
        onSubscribe: subscription => {
          subscription.request(2147483647);
        },
      });
    },
    onError: error => {
      console.log(error);
      addErrorMessage("Connection has been refused due to ", error);
    },
    onSubscribe: cancel => {
      /* call cancel() to abort */
    }
  });
Yuri Schimke
  • 12,435
  • 3
  • 35
  • 69
0

rsocket in Spring is like API as a Web Socket (https://rsocket.io/), it offers an API-type (over TCP) service for use by client applications. So, an endpoint is some URI that you call to get some data resource. So, it is a URI something like this: ws://localhost:8080/WebsocketTest/testserver OR yourtag://url:portnumber/resource OR it can even be localhost if you are testing locally.

The endpoint is ideally called with the clientId. It depends on your rsocket design. Also, what is it that you are trying to do here? What service are you trying to offering via the rsocket?

You can find some experts on the topic here: https://gitter.im/rsocket/rsocket-java