Search code examples
typescriptspring-messagingrsocket

Unable to read the payload of a requestResponse call at the server side


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.


Solution

  • 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 */
        }
      });