1

everyone.

Question 1

I try to use Rsocket. How does Rsocket server send message to client? Like websocket, the server can send message to one client or all clients. I can not find a way to do it. Maybe I can use Channel? But I do not know what to do.

Question 2

I use Channel like this:

  @MessageMapping("channel")
  fun channel(settings: Flux<Duration>) =
      settings
          .doOnNext { setting: Duration -> println("Frequency setting is ${setting.seconds} second(s).") }
          .switchMap { setting: Duration ->
            Flux.interval(setting).map { index: Long -> Message("Server", "Channel", index) }
          }
          .log()

And in js, I do this:

const {
  RSocketClient
} = require('rsocket-core');
const RSocketTcpClient = require('rsocket-tcp-client').default;
const {Flowable} = require('rsocket-flowable')

const tcpClient = new RSocketTcpClient((host, port))

const client = new RSocketClient({
  setup: {
    keepAlive,
    lifetime,
    dataMimeType: "application/json",
    metadataMimeType: 'message/x.rsocket.routing.v0'
  },
  transport: tcpClient,
});

const flowablePayload = new Flowable(subscriber => {
  subscriber.onSubscribe({
    cancel: () => {},
    request: n => {
      for (let index = 0; index < n; index++) {
        const message = {
          message: "requestChannel from JavaScript! #" + index
        };
        subscriber.onNext(message);
      }
      subscriber.onComplete();
    }
  });
});    

client.connect().subscribe({
  onComplete: socket => {
    console.log('onComplete')
    socket.requestChannel({
      data: flowablePayload,
      metadata: String.fromCharCode('channel'.length) + 'channel'
    }).subscribe({
      onComplete: res => console.log(res),
      onError: err => console.error(err),
      onSubscribe: cancel => console.log('success')
    });
  },
  onError: error => {
    console.log("got error");
    console.error(error);
  },
  onSubscribe: cancel => {
    console.log("subscribe!");
  }
})

output:

subscribe!
onComplete
success

But I can not get the MessageMapping channel info and channel method is not executed.

What should I do?

Thanks!!!

EchoCow
  • 163
  • 8
  • hi @EchoCow, I'm struggling exactly the same issue as you. Can we chat about it, maybe we will be able to figure it out. https://chat.stackoverflow.com/rooms/212325/rsocket-with-spring-webflux – kojot Apr 23 '20 at 07:50

1 Answers1

0

Question 1: If you want to know how to send a message to a certain client, please see this StackOverflow question Send message only to certain client using websockets with Rsocket and Spring Webflux

Question 2: I've never tried using the channels, but I'm not sure you really need it. I would use the channel for example in an online game, where the user constantly pushes the keys and the server has to respond to them.

kojot
  • 1,634
  • 2
  • 16
  • 32