websocket method
Implementation
Stream<dynamic> websocket({
required String baseDataUrl,
required String mentor,
required String sessionId,
required String token,
required String username,
String? prompt, // Optional prompt
}) {
var channel = WebSocketChannel.connect(
Uri.parse('wss://$baseDataUrl/ws/langflow/'),
);
// Initial data without the prompt
final dataWithoutPrompt = {
"flow": {"name": mentor, "tenant": 'main', "username": username},
"session_id": sessionId,
"token": token
};
// Send initial data without prompt
channel.sink.add(json.encode(dataWithoutPrompt));
// Wait for three seconds before sending the prompt
Future.delayed(Duration(seconds: 3), () {
// Check if prompt is provided and send it
if (prompt != null) {
final dataWithPrompt = {...dataWithoutPrompt, "prompt": prompt};
channel.sink.add(json.encode(dataWithPrompt));
}
});
// Return a stream that handles incoming messages
return channel.stream.map((message) {
return jsonDecode(message);
}).handleError((error) {
print('Error connecting to server: $error');
channel.sink.close();
});
}