The DifyChatClient allows you to interact with chat-based applications. You can use Blocking Mode for simple request-response cycles or Streaming Mode to receive real-time message chunks via a callback.
Blocking Mode
Use sendChatMessage(message) to wait for the full response. Set responseMode(ResponseMode.BLOCKING) in the ChatMessage builder.
Streaming Mode
Use sendChatMessageStream(message, callback) to handle real-time updates. Implement ChatStreamCallback to handle onMessage (chunks), onMessageEnd (completion), onError, and onException events.
// Blocking Mode
DifyChatClient chatClient = DifyClientFactory.createChatClient("https://api.dify.ai/v1", "your-api-key");
ChatMessage message = ChatMessage.builder()
.query("Hello, please introduce yourself")
.user("user-123")
.responseMode(ResponseMode.BLOCKING)
.build();
ChatMessageResponse response = chatClient.sendChatMessage(message);
// Streaming Mode
chatClient.sendChatMessageStream(message, new ChatStreamCallback() {
@Override
public void onMessage(MessageEvent event) {
System.out.println("Received message chunk: " + event.getAnswer());
}
// ... other overrides
});