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

mraible opened this issue Sep 24, 2018 · 5 comments


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: ' +;

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'}),

  onComplete: (socket: any) => {
  onError: (error: any) => console.error(error),
  onNext(payload: any) {

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 does?


This comment has been minimized.

Copy link

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).


This comment has been minimized.

Copy link

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


This comment has been minimized.

Copy link

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.


This comment has been minimized.

Copy link

commented Feb 21, 2019

I'm the 3rd


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

class ReactiveController {
  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();
  data: {...},
  metadata: 'my-message'

Key part is the metadataMimeType (see 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
None yet
5 participants
You can’t perform that action at this time.