Websocket 1-1 chat between authenticated users

Hello i'm wondering how to send message to a specific logged in user using spring web sockets I have assigned a UUID to each user when he signups, and after login each user gets a JWT token for session authentication But i'm trying for a long time fixing a 1-1 chat between users that exists in the db. For example if i have 2 users logged in, i can then click on the user then it should subscribe to a chat room between them and i can then chat in real time. I have been confused for a long time since there isnt alot of documents of this. If someone can either guide me through this process real quick or maybe explain it fast it would be awesome! thanks.
9 Replies
JavaBot
JavaBotā€¢9mo ago
āŒ› This post has been reserved for your question.
Hey @Itsurran! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Itsurran
ItsurranOPā€¢9mo ago
Note: I'm using Reactjs for the client.
szatkus
szatkusā€¢9mo ago
Message queue You can do some resume-driven development and use Kafka for that.
Itsurran
ItsurranOPā€¢9mo ago
what are you talking about?
szatkus
szatkusā€¢9mo ago
You need a message queue to send messages between users. Then you can subscribe to that from web sockets and transport the messages between two users.
Itsurran
ItsurranOPā€¢9mo ago
Yeah I know how it works but Iā€™m not getting the process of doing it. I have a Websocketconfig and a controller
package websocket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.socket.WebSocketSession;

import java.security.Principal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Controller
public class WebSocketController {
private final Map<String, WebSocketSession> userSessions = new ConcurrentHashMap<>();

@Autowired
private SimpMessagingTemplate messagingTemplate;

@MessageMapping("/chat/{recipientId}")
public void sendMessage(@DestinationVariable String recipientId, Message message, Principal principal) {
WebSocketSession recipientSession = userSessions.get(recipientId);
if (recipientSession != null && recipientSession.isOpen()) {

messagingTemplate.convertAndSendToUser(recipientId, "/queue/messages", message);
} else {

}
}
@SubscribeMapping("/chat/{senderId}")
public void subscribeToUser(@DestinationVariable String senderId, Principal principal) {
userSessions.put(senderId, (WebSocketSession) principal);
}
}
package websocket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.socket.WebSocketSession;

import java.security.Principal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Controller
public class WebSocketController {
private final Map<String, WebSocketSession> userSessions = new ConcurrentHashMap<>();

@Autowired
private SimpMessagingTemplate messagingTemplate;

@MessageMapping("/chat/{recipientId}")
public void sendMessage(@DestinationVariable String recipientId, Message message, Principal principal) {
WebSocketSession recipientSession = userSessions.get(recipientId);
if (recipientSession != null && recipientSession.isOpen()) {

messagingTemplate.convertAndSendToUser(recipientId, "/queue/messages", message);
} else {

}
}
@SubscribeMapping("/chat/{senderId}")
public void subscribeToUser(@DestinationVariable String senderId, Principal principal) {
userSessions.put(senderId, (WebSocketSession) principal);
}
}
Here is the controller and here is the config
package websocket;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {

registry.addEndpoint("/chat").setAllowedOrigins("*").withSockJS();
}
}
package websocket;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {

registry.addEndpoint("/chat").setAllowedOrigins("*").withSockJS();
}
}
Anyone?
JavaBot
JavaBotā€¢9mo ago
šŸ’¤ Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
dan1st
dan1stā€¢9mo ago
What's the problem with that?
JavaBot
JavaBotā€¢9mo ago
šŸ’¤ Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?