Invalid argument(s): Unsupported scheme ‘wss’ in URI wss://wsportfolioupdate-api.upstox.com/portfolio-stream-feeder/v2/upstox-developer-api/feeds?requestId=db20733e-6e10-4ba3-9eb0-285184e2f534&code=UBC05-3b7aa7c9-ee8c-469f-920c-98ae138925f6 this is the error iam getting when connecting websocket(endpoints) in flutter
wss://api.upstox.com/v2/feed/portfolio-stream-feed this is the end point
In Flutter application, you can use the web_socket_channel package. You would have to use the WebSocketChannel.connect
in to connect, here is the sample code
final channel = WebSocketChannel.connect( Uri.parse('[wss://example.com](wss://example.com/)'), );
I hope this helps. You can refer here for more details.
1 Like
iam using that package but iam getting error should i share the code screen shot or can i contact to you personally?
In your error message that you shared below
Invalid argument(s): Unsupported scheme ‘wss’ in URI wss://wsportfolioupdate-api.upstox.com/portfolio-stream-feeder/v2/upstox-developer-api/feeds?requestId=db20733e-6e10-4ba3-9eb0-285184e2f534&code=UBC05-3b7aa7c9-ee8c-469f-920c-98ae138925f6
I see the URI as wss://wsportfolioupdate-api.upstox.com/portfolio-stream-feeder/v2/upstox-developer-api/feeds
could you please correct it to wss://api.upstox.com/v2/feed/portfolio-stream-feed
If that also doesn’t work please share the code snippet, make sure you redact any secret info if any.
// final headers = {
// ‘Authorization’: ‘Bearer ${widget.accessToken}’,
// ‘Accept’: ‘/’,
// };
// final body = {
// ‘guid’: ‘someguid’,
// ‘method’: ‘sub’,
// ‘data’: {
// ‘mode’: ‘ltpc’,
// ‘instrumentKeys’: [
// ‘NSE_INDEX|Nifty Bank’
// ], // Replace this with the actual instrument key
// },
// };
// channel = await connectToWebSocket(
// url: ‘wss://api.upstox.com/v2/feed/market-data-feed’,
// headers: headers,
// body: body);
// Future connectWebSSocket() async {
// try {
// print(‘fff’);
// WebSocket webSocket;
// channel = IOWebSocketChannel.connect(
// Uri.parse(
// ‘wss://wsfeeder-api.upstox.com/market-data-feeder/v2/upstox-developer-api/feeds?requestId=262fe601-0bf3-4683-b4ff-aedd8eb4ea2f&code=S2T5j-e39a72bb-9c9f-46c7-a7c1-b16bda6b3a7b’),
// headers: {
// ‘Authorization’: ‘Bearer ${widget.accessToken}’,
// ‘Accept’: ‘/’
// },
// );
// channel!.stream.listen(
// (data) {
// print(‘Received data: $data’);
// setState(() {
// // Handle received data
// });
// },
// onError: (error) {
// print(‘WebSocket error: $error’);
// },
// onDone: () {
// print(‘WebSocket closed’);
// },
// );
// } catch (e) {
// print(‘ff $e’);
// }
// }
@Ninad_Malvankar this is code i used for websocket connection. please go through it and let me know if anything wrong in that. thanks
Based on the code snippet that you have shared I see that the connect method has a different URI (wss://wsfeeder-api.upstox.com/market-data-feeder/v2/upstox-developer-api/feeds?requestId=262fe601-0bf3-4683-b4ff-aedd8eb4ea2f&code=S2T5j-e39a72bb-9c9f-46c7-a7c1-b16bda6b3a7b)
You have used the correct one when you are calling the function connectToWebSocket
but that function does not exists in your code snippet but a different one exists connectWebSSocket
where there is an S
before the Socket.
Could you please correct your code accordingly.
sorry for my inconvenience happend this the code and error iam getting please go through it.
void connectWebS() {
const guid = ‘someguid’;
const method = ‘sub’;
const mode = ‘full’;
const instrumentKeys = ‘NSE_INDEX|Nifty Bank’;
final params = {
'guid': guid,
'method': method,
'mode': mode,
'instrumentKeys': instrumentKeys,
};
final headers = {
'Authorization': 'Bearer ${widget.accessToken}',
'Accept': '*/*'
};
final uri = Uri(
scheme: 'wss',
host: 'wsfeeder-api.upstox.com',
path: 'v2/feed/market-data-feed',
queryParameters: params,
);
print('Connecting to WebSocket: $uri');
final channel =
IOWebSocketChannel.connect(uri.toString(), headers: headers);
channel.stream.listen((mesg) {}, onError: (e) {
print('err ${e.toString()}');
});
}
Error:
Unhandled Exception: WebSocketChannelException: WebSocketException: Connection to ‘https://wsfeeder-api.upstox.com:0/v2/feed/market-data-feed?guid=someguid&method=sub&mode=full&instrumentKeys=NSE_INDEX|Nifty+Bank#’ was not upgraded to websocket
One thing that I have noticed is that for this WS we have a redirection which is mentioned in the documentation.
Unfortunately the web_socket_channel
package in Dart, which is commonly used in Flutter, does not natively support HTTP redirect following for WebSocket connections. So in order to follow redirects for WebSocket connections, you’ll need to handle the redirection manually. This involves making an initial HTTP request to the server, following any redirects manually, and then establishing the WebSocket connection to the final URL.
You can try something like this
WebSocketChannel? _channel;
final params = {
'guid': guid,
'method': method,
'mode': mode,
'instrumentKeys': instrumentKeys,
};
final headers = {
'Authorization': 'Bearer ${widget.accessToken}',
'Accept': '*/*'
};
Future<void> _connectToWebSocket() async {
try {
// Initial WebSocket URL
final initialUrl = 'wss://api.upstox.com/v2/feed/market-data-feed';
// Convert to HTTP URL for redirection handling
final httpUrl = initialUrl.replaceFirst('wss://', 'https://').replaceFirst('ws://', 'http://');
Uri urlWithParams = Uri.parse(httpUrl).replace(queryParameters: params);
// Make the initial HTTP request
var response = await http.get(Uri.parse(urlWithParams), headers: headers);
// Follow redirects if necessary
while (response.isRedirect) {
final location = response.headers['location'];
if (location == null) break;
response = await http.get(Uri.parse(location), headers: headers);
}
// Get the final WebSocket URL
final finalUrl = response.request?.url.toString().replaceFirst('https://', 'wss://').replaceFirst('http://', 'ws://');
// Establish WebSocket connection
if (finalUrl != null) {
_channel = WebSocketChannel.connect(Uri.parse(finalUrl));
_channel?.stream.listen(
(message) {
print("Received: $message");
},
onDone: () {
print("WebSocket is closed");
},
onError: (error) {
print("WebSocket error: $error");
},
);
}
} catch (e) {
print("Error connecting to WebSocket: $e");
}
}
NOTE: You might have to tweek the code a bit.
@Ninad_Malvankar
after running your code this is the error iam getting Unsupported scheme ‘wss’ in URI wss://wsfeeder-api.upstox.com/market-data-feeder/v2/upstox-developer-api/feeds?requestId=ce9b65b4-7231-4a7a-9805-be2863cec8f1&code=gDqF5-52c39fc6-f537-4504-9039-d6cf9a8ae94c
Can you try without the while block.
WebSocketChannel? _channel;
final params = {
'guid': guid,
'method': method,
'mode': mode,
'instrumentKeys': instrumentKeys,
};
final headers = {
'Authorization': 'Bearer ${widget.accessToken}',
'Accept': '*/*'
};
Future<void> _connectToWebSocket() async {
try {
// Initial WebSocket URL
final initialUrl = 'wss://api.upstox.com/v2/feed/market-data-feed';
// Convert to HTTP URL for redirection handling
final httpUrl = initialUrl.replaceFirst('wss://', 'https://').replaceFirst('ws://', 'http://');
Uri urlWithParams = Uri.parse(httpUrl).replace(queryParameters: params);
// Make the initial HTTP request
var response = await http.get(Uri.parse(urlWithParams), headers: headers);
// Get the final WebSocket URL
final finalUrl = response.request?.url.toString().replaceFirst('https://', 'wss://').replaceFirst('http://', 'ws://');
// Establish WebSocket connection
if (finalUrl != null) {
_channel = WebSocketChannel.connect(Uri.parse(finalUrl));
_channel?.stream.listen(
(message) {
print("Received: $message");
},
onDone: () {
print("WebSocket is closed");
},
onError: (error) {
print("WebSocket error: $error");
},
);
}
} catch (e) {
print("Error connecting to WebSocket: $e");
}
}
@Ninad_Malvankar thanks for your response i tried the code you sent and changed a bit to have better understanding about where we are getting error from so, below is the code and error.
can i know where why iam getting this error so that i can research about that
code :
Future _connectToWebSocket() async {
try {
print(‘1’);
const guid = ‘someguid’;
const method = ‘sub’;
const mode = ‘full’;
const instrumentKeys = ‘NSE_INDEX|Nifty Bank’;
final params = {
'guid': guid,
'method': method,
'mode': mode,
'instrumentKeys': instrumentKeys,
};
final headers = {
'Authorization': 'Bearer ${widget.accessToken}',
'Accept': '*/*'
};
print(‘2’);
// Initial WebSocket URL
final initialUrl = ‘wss://api.upstox.com/v2/feed/market-data-feed’;
print(‘3’);
// Convert to HTTP URL for redirection handling
final httpUrl = initialUrl
.replaceFirst(‘wss://’, ‘https://’)
.replaceFirst(‘ws://’, ‘http://’);
print(‘4’);
Uri urlWithParams = Uri.parse(httpUrl).replace(queryParameters: params);
print(‘5’);
// Make the initial HTTP request
var response =
await http.get(Uri.parse(urlWithParams.toString()), headers: headers);
print(‘6’);
// Get the final WebSocket URL
final finalUrl = response.request?.url
.toString()
.replaceFirst(‘https://’, ‘wss://’)
.replaceFirst(‘http://’, ‘ws://’);
print(‘7’);
// Establish WebSocket connection
if (finalUrl != null) {
channel = WebSocketChannel.connect(Uri.parse(finalUrl));
channel?.stream.listen(
(message) {
print(“Received: $message”);
},
onDone: () {
print(“WebSocket is closed”);
},
onError: (error) {
print(“WebSocket error: $error”);
},
);
}
} catch (e) {
print(“Error connecting to WebSocket: $e”);
}
}
response:
I/flutter ( 5396): 1
I/flutter ( 5396): 2
I/flutter ( 5396): 3
I/flutter ( 5396): 4
I/flutter ( 5396): 5
I/flutter ( 5396): Error connecting to WebSocket: Invalid argument(s): Unsupported scheme ‘wss’ in URI wss://wsfeeder-api.upstox.com/market-data-feeder/v2/upstox-developer-api/feeds?requestId=0a8478fc-4e33-47c9-9c10-5a835ac0e7cc&code=R9BuV-6659547d-518c-48ef-9769-58f89a1e5d49
Please try this and let me know if this helps
WebSocketChannel? _channel;
final params = {
'guid': guid,
'method': method,
'mode': mode,
'instrumentKeys': instrumentKeys,
};
final headers = {
'Authorization': 'Bearer ${widget.accessToken}',
'Accept': '*/*'
};
Future<void> _connectToWebSocket() async {
try {
// Initial WebSocket URL
final initialUrl = 'wss://api.upstox.com/v2/feed/market-data-feed';
// Convert to HTTP URL for redirection handling
final httpUrl = initialUrl.replaceFirst('wss://''https://').replaceFirst('ws://', 'http://')
Uri urlWithParams = Uri.parse(httpUrl).replace(queryParameters: params);
final client = HttpClient();
var uri = Uri.parse(urlWithParams);
var request = await client.getUrl(uri);
request.followRedirects = false;
// Add headers to the request
headers.forEach((name, value) {
request.headers.add(name, value);
});
final response = await request.close();
// Handle the response
if (response.statusCode == HttpStatus.movedPermanently || response.statusCode == HttpStatus.found) {
// Handle the redirect manually if needed
final finalUrl = response.headers.value(HttpHeaders.locationHeader);
// Establish WebSocket connection
if (finalUrl != null) {
_channel = WebSocketChannel.connect(Uri.parse(finalUrl));
_channel?.stream.listen(
(message) {
print("Received: $message");
},
onDone: () {
print("WebSocket is closed");
},
onError: (error) {
print("WebSocket error: $error");
},
);
}
}
} catch (e) {
print("Error connecting to WebSocket: $e");
}
}
Here I have disabled the followRedirects
from the httpClient
as by default when we use http.get
it is enabled.
References:
@Ninad_Malvankar no response brother
@Ninad_Malvankar hey brother what to do now. i tried the code you sent iam not getting any response. can i know why it is not working? what is actual reason
@Ninad_Malvankar brother what will be the solution for this. please help me in this
We can offer only limited support or guidance for developing custom applications that extend the core functionality of the Upstox API.
For Flutter WebSocket development, I recommend consulting an expert, as WebSockets can be complex to manage. We might consider developing a Flutter SDK, but it’s not currently part of our plans.
We are happy to prioritize any concerns or feedback about the core API features.
@Ninad_Malvankar i tried reaching expert but i got the same response error so please try to understand from our end i tried several times with code change but nothing worked so now expecting help from your side itself cause i couldnt find any
Portfolio Stream Feed Authorize Url | Upstox Developer API try this method instead. you will get a wss url as json , you may use that to connect and get authorized with that one time link
1 Like