Reputation: 85
I am working on a mobile application using flutter and dart. I need to establish websocket connection with backend which is implemented using Asp.net SiganlR. Backend is implemented by other team and I cannot modify/update it. The websocket communication is protected at the backend and I need to provide access token in order to access the service.
I am trying this code to establish a communication channel:
IOWebSocketChannel _channel = new IOWebSocketChannel.connect('wss://localhost:5000/sinkhub', headers: {
'X-TOKEN': 'token'
});
But I am not able to establish the connection and end up having error. This is the screenshot of the _channel object after executing above code.
Any help in this regard is appreciated. More specifically, how to connect to signalR backend from flutter app. Most important point to note is, backend is protected and I need to send access token in order to access the service.
Upvotes: 3
Views: 3407
Reputation: 2052
I am using package signalr_client 0.1.6
Lets say you are storing token in local storage. So create a method to retrieve it like below.
Future<String> getAccessToken() async {
FlutterSecureStorage _storage = FlutterSecureStorage();
return await _storage.read(key: 'token');
}
Now you need to use this method when you are building your hub connection.
Future startConnection() async {
connection = HubConnectionBuilder()
.withUrl('https://************/hubs/chatHub',
options: HttpConnectionOptions(
accessTokenFactory: () async =>
await getAccessToken()))
.build();
await connection.start();
}
Upvotes: 1