Skip to content

Commit 9ffd334

Browse files
Add WebSocket endpoint for chatbot and Todo.description
Will use WebSocket for bidirectional message to and from server Added a description for Todo for the new UI
1 parent f0c9516 commit 9ffd334

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
<groupId>io.quarkus</groupId>
4242
<artifactId>quarkus-rest-jsonb</artifactId>
4343
</dependency>
44+
<dependency>
45+
<groupId>io.quarkus</groupId>
46+
<artifactId>quarkus-websockets-next</artifactId>
47+
</dependency>
4448
<dependency>
4549
<groupId>io.quarkus</groupId>
4650
<artifactId>quarkus-smallrye-openapi</artifactId>

src/main/java/io/quarkus/sample/Todo.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ public class Todo extends PanacheEntity {
1616
@Column(unique = true)
1717
public String title;
1818

19+
public String description;
20+
1921
public boolean completed;
2022

2123
@Column(name = "ordering")
2224
public int order;
2325

26+
2427
@Schema(example = "https://github.com/quarkusio/todo-demo-app")
2528
public String url;
2629

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.quarkus.sample;
2+
3+
import io.smallrye.mutiny.Multi;
4+
import io.smallrye.mutiny.subscription.MultiEmitter;
5+
import jakarta.enterprise.context.control.ActivateRequestContext;
6+
7+
import io.quarkus.websockets.next.OnOpen;
8+
import io.quarkus.websockets.next.OnTextMessage;
9+
import io.quarkus.websockets.next.PathParam;
10+
import io.quarkus.websockets.next.WebSocket;
11+
12+
13+
@WebSocket(path = "/todo-agent/{todoId}")
14+
@ActivateRequestContext
15+
public class TodoAgentWebSocket {
16+
private MultiEmitter<? super String> emitter;
17+
private Multi<String> agentStream;
18+
19+
@OnOpen
20+
Multi<String> onOpen(@PathParam String todoId) {
21+
this.agentStream = Multi.createFrom().emitter(emitter -> {
22+
this.emitter = emitter;
23+
// The emitter is now available for use elsewhere
24+
});
25+
emitter.emit("ok we will find agents for todo " + todoId );
26+
return agentStream;
27+
}
28+
29+
@OnTextMessage
30+
void onTextMessage(@PathParam String todoId, String message) {
31+
//do something with the agent context
32+
emitter.emit("Parroting for " + todoId + " : " + message);
33+
}
34+
}

src/main/resources/import.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
INSERT INTO todo(id, title, completed, ordering, url) VALUES (nextval('todo_seq'), 'Introduction to Quarkus', true, 0, null);
22
INSERT INTO todo(id, title, completed, ordering, url) VALUES (nextval('todo_seq'), 'Hibernate with Panache', false, 1, null);
33
INSERT INTO todo(id, title, completed, ordering, url) VALUES (nextval('todo_seq'), 'Visit Quarkus web site', false, 2, 'https://quarkus.io');
4-
INSERT INTO todo(id, title, completed, ordering, url) VALUES (nextval('todo_seq'), 'Star Quarkus project', false, 3, 'https://github.com/quarkusio/quarkus/');
4+
INSERT INTO todo(id, title, completed, ordering, description, url) VALUES (nextval('todo_seq'), 'Star Quarkus project', false, 3, 'Go to GitHub. Find quarkus in the search engine. Star the project. And mention it to your friends!', 'https://github.com/quarkusio/quarkus/');

src/test/java/io/quarkus/sample/TodoResourceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private static Stream<Arguments> todoItemsToDelete() {
8787
Arguments.of(15, 404));
8888
}
8989

90-
private static final String ALL = "[{\"id\":1,\"completed\":true,\"order\":0,\"title\":\"Introduction to Quarkus\"},{\"id\":51,\"completed\":false,\"order\":1,\"title\":\"Hibernate with Panache\"},{\"id\":101,\"completed\":false,\"order\":2,\"title\":\"Visit Quarkus web site\",\"url\":\"https://quarkus.io\"},{\"id\":151,\"completed\":false,\"order\":3,\"title\":\"Star Quarkus project\",\"url\":\"https://github.com/quarkusio/quarkus/\"}]";
90+
private static final String ALL = "[{\"id\":1,\"completed\":true,\"order\":0,\"title\":\"Introduction to Quarkus\"},{\"id\":51,\"completed\":false,\"order\":1,\"title\":\"Hibernate with Panache\"},{\"id\":101,\"completed\":false,\"order\":2,\"title\":\"Visit Quarkus web site\",\"url\":\"https://quarkus.io\"},{\"id\":151,\"completed\":false,\"description\":\"Go to GitHub. Find quarkus in the search engine. Star the project. And mention it to your friends!\",\"order\":3,\"title\":\"Star Quarkus project\",\"url\":\"https://github.com/quarkusio/quarkus/\"}]";
9191

9292
private static final String ONE = "{\"id\":1,\"completed\":true,\"order\":0,\"title\":\"Introduction to Quarkus\"}";
9393

0 commit comments

Comments
 (0)