React Native and WebSocket communication - StompJS will not connect for me, alternatives?

29 views Asked by At

I have a springboot application that is hosting my websocket endpoints, I can connect to the endpoints and subscribe to a specific topic and get the results perfectly fine using React and StompJS and also Insomina API client.

I want the same functionality in my React Native application but I dont think StompJS is compatible with a React Native project, I could be wrong. but I have spent days trying to solve this problem. Whenever I try to connect from my React Native app I get no further than "LOG: Opening Web Socket...".

I intially thought this was either due to a CORS issue or perhaps my local environment setup. To rule out my local environment I pointed my mobile app at a www service that is know to work just fine, I had the same results, hangs with "LOG: Opening Web Socket...".

I also tried with the react-stomp-hooks library but exactly the same results.

I have reverted back to naitive/inbuilt WebSocket object:

const ws = new WebSocket('ws://my.websocket.service/ws');

This method does connect to the websocket service however I am not receiving any data through to my ws.onmessage() method.

I have 2 questions.

  • Do you know of a library that specifically works with React Native and allows you to connect to a websocket service and also allows you to subscribe to specific topics. Should StompJS work in React Native? It is just what I need if it does!

  • Using the native WebSocket() object like shown above, how would I subscribe to a specific topic such as "/topic/newsfeed", I have read this may possible by supplying a query but and example would be great.

I have ruled out the following:

  • it is not a CORS issue
  • it is not a http/https issue
  • the following config does nothing for me as suggested in other answers
    stompConfig: {
        forceBinaryWSFrames: true,
        appendMissingNULLonIncoming: true,
    }
    
0

There are 0 answers