Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide simple websocket example #21

Closed
mraible opened this issue Sep 24, 2018 · 5 comments

Comments

@mraible
Copy link

commented Sep 24, 2018

I have a simple websocket example that I'm trying to implement with rsocket-js (since I'm connecting to a Spring WebFlux-powered websocket endpoint).

This works using plain ol' websockets:

const socket = new WebSocket('ws://localhost:8080/ws/profiles');
socket.addEventListener('message', (event: any) => {
    window.alert('message from server: ' + event.data);
});

From the current docs, I figured this is equivalent to the following.

// Create an instance of a client
const client = new RSocketClient({
  // send/receive objects instead of strings/buffers
  serializers: JsonSerializers,
  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: 'application/json',
  },
  transport: new RSocketWebSocketClient({url: 'ws://localhost:8080/ws/profiles'}),
});

client.connect().subscribe({
  onComplete: (socket: any) => {
    socket.requestResponse();
  },
  onError: (error: any) => console.error(error),
  onNext(payload: any) {
    console.log('onNext(%s)', payload.data);
  },
});

However, I don't get any console logs when I trigger a new websocket event.

Related: does rsocket-js support sending an authorization header in the request like socket.io-client does?

@josephsavona

This comment has been minimized.

Copy link
Contributor

commented Sep 24, 2018

Does your server support the rsocket protocol? Note that rsocket is a higher level protocol that can use websocket as a transport - neither the client or server can start pushing arbitrary data to the other (as in your WS example) unless it initiates one of the defined actions (request-response, request-stream, etc). In this case the client would typically make a requestResponse with a payload indicating the data it is requesting, and the server would reply with one response (to send multiple payloads use request stream). The corresponding response payload would be accessible via the onComplete handler to requestResponse (missing in your second example).

@mraible

This comment has been minimized.

Copy link
Author

commented Sep 24, 2018

@josephsavona It must not. There is an open ticket for WebFlux to support it, so I guess I'll have to wait until that's finished.

@mraible mraible closed this Sep 24, 2018

@yschimke

This comment has been minimized.

Copy link
Member

commented Oct 2, 2018

I think this has been a point of confusion twice now. We should probably make this clearer in the docs if it comes up again.

@walter211

This comment has been minimized.

Copy link

commented Feb 21, 2019

I'm the 3rd

@eriktim

This comment has been minimized.

Copy link

commented Jun 27, 2019

I also ran into difficulties getting rsocket to work over WebSockets using a Spring back-end. I ran into this issue so in case anyone needs it here is what I did.

In Java:

// spring.rsocket.server.mapping-path=/rsocket

@Controller
class ReactiveController {
  @MessageMapping('my-message')
  Flux<String> myMessage() {
    return Flux.just("hello world");
  }
}

In JavaScript:

const client = new RSocketClient({
  serializers: {
    data: JsonSerializer,
    metadata: IdentitySerializer
  },
  setup: {
    dataMimeType: 'application/json',
    keepAlive: 100000,
    lifetime: 100000,
    metadataMimeType: 'message/x.rsocket.routing.v0',
  },
  transport: new RSocketWebSocketClient({
    url: 'ws://localhost:8080/rsocket'
  })
});
const socket = await client.connect();
socket.requestStream({
  data: {...},
  metadata: 'my-message'
}).subscribe(...)'

Key part is the metadataMimeType (see https://github.com/rsocket/rsocket/blob/master/Extensions/Routing.md) and the metadata value that ensures messages are matched to the right endpoint using Spring's @MessageMapping annotation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.