user3473445
user3473445

Reputation: 194

How to subscribe to a Websocket topic in flutter?

The problem is that I can't find a solution for subscribing to a WebSocket topic.

I've tried web_socket_channel, but there was no needed functionality for subscribing to a specific topic.

Also, I've tried stomp_sockjs, but it requires SDK version <2.0.0. My current Dart SDK version is 2.4.0 and I don't want to lower it because it would break all the other dependencies I have.

In other WebSocket libraries like adhara_socket_io, I can't find a method to subscribe for a topic.

I managed to consume the data from WebSocket using JS with help of sockjs and stomp libraries. But how to do that in flutter?

function connect() {
            stompClient = Stomp.client("ws://xx.xxx.xxx.xxx/somePath");
            stompClient.connect({"Access-Control-Allow-Origin":"*"}, function (frame) {
                setConnected(true);
                console.log('Connected: ' + frame);
                stompClient.subscribe('/topic/someStatus', function (greeting) {
                    showGreeting(JSON.parse(greeting.body));
                });
            });
        }

So, I want to find a solution that would do the same but in my flutter app

Upvotes: 1

Views: 6185

Answers (2)

user3473445
user3473445

Reputation: 194

I'have found the solution: https://github.com/killerWqs2/flutter-websocket-client

It allows subscribing to a stomp topic via WebSockets

Upvotes: 1

Danny Tuppeny
Danny Tuppeny

Reputation: 42413

WebSockets don't have a concept of topics - the protocol you're using in JS is called STOMP so you'd need to use a STOMP library for Dart.

There are some STOMP packages on Pub, but I don't know how complete (or trustworthy) they are:

The S in STOMP stands for Simple though, so if neither work it might not be too difficult to implement yourself.

Upvotes: 2

Related Questions