Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f9ff2f0

Browse files
author
Rajeev Kumar Singh
committed
Added Example for a RabbitMQ message broker
1 parent 6512583 commit f9ff2f0

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

‎pom.xml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.springframework.boot</groupId>
1616
<artifactId>spring-boot-starter-parent</artifactId>
17-
<version>1.5.4.RELEASE</version>
17+
<version>1.5.9.RELEASE</version>
1818
<relativePath/> <!-- lookup parent from repository -->
1919
</parent>
2020

@@ -29,6 +29,28 @@
2929
<groupId>org.springframework.boot</groupId>
3030
<artifactId>spring-boot-starter-websocket</artifactId>
3131
</dependency>
32+
33+
<!-- RabbitMQ Starter Dependency (Not required if you're using the simple in-memory broker for STOMP) -->
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-amqp</artifactId>
37+
</dependency>
38+
39+
<!-- Following three dependencies are required for STOMP Broker Relay -->
40+
<dependency>
41+
<groupId>io.projectreactor</groupId>
42+
<artifactId>reactor-core</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>io.projectreactor</groupId>
46+
<artifactId>reactor-net</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>io.netty</groupId>
50+
<artifactId>netty-all</artifactId>
51+
<version>4.1.19.Final</version>
52+
</dependency>
53+
3254
<dependency>
3355
<groupId>org.springframework.boot</groupId>
3456
<artifactId>spring-boot-devtools</artifactId>

‎src/main/java/com/example/websocketdemo/config/WebSocketConfig.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ public void registerStompEndpoints(StompEndpointRegistry registry) {
1919
@Override
2020
public void configureMessageBroker(MessageBrokerRegistry registry) {
2121
registry.setApplicationDestinationPrefixes("/app");
22-
registry.enableSimpleBroker("/channel");
22+
registry.enableSimpleBroker("/topic"); // Enables a simple in-memory broker
23+
24+
25+
// Use this for enabling a Full featured broker like RabbitMQ
26+
registry.enableStompBrokerRelay("/topic")
27+
.setRelayHost("localhost")
28+
.setRelayPort(61613)
29+
.setClientLogin("guest")
30+
.setClientPasscode("guest");
2331
}
2432
}

‎src/main/java/com/example/websocketdemo/controller/ChatController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
public class ChatController {
1515

1616
@MessageMapping("/chat.sendMessage")
17-
@SendTo("/channel/public")
17+
@SendTo("/topic/public")
1818
public ChatMessage sendMessage(@Payload ChatMessage chatMessage) {
1919
return chatMessage;
2020
}
2121

2222
@MessageMapping("/chat.addUser")
23-
@SendTo("/channel/public")
23+
@SendTo("/topic/public")
2424
public ChatMessage addUser(@Payload ChatMessage chatMessage,
2525
SimpMessageHeaderAccessor headerAccessor) {
2626
// Add username in web socket session

‎src/main/java/com/example/websocketdemo/controller/WebSocketEventListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void handleWebSocketDisconnectListener(SessionDisconnectEvent event) {
3939
chatMessage.setType(ChatMessage.MessageType.LEAVE);
4040
chatMessage.setSender(username);
4141

42-
messagingTemplate.convertAndSend("/channel/public", chatMessage);
42+
messagingTemplate.convertAndSend("/topic/public", chatMessage);
4343
}
4444
}
4545
}

‎src/main/resources/static/js/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ function connect(event) {
3333

3434

3535
function onConnected() {
36-
// Subscribe to the Public Channel
37-
stompClient.subscribe('/channel/public', onMessageReceived);
36+
// Subscribe to the Public Topic
37+
stompClient.subscribe('/topic/public', onMessageReceived);
3838

3939
// Tell your username to the server
4040
stompClient.send("/app/chat.addUser",

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /