0

I want to know how to define the subscriber path.

For instance, declaration of subscribing path

    stompClient.subscribe("/topic/simplemessagesresponse", function(servermessage) {

Why there are two parts 'topic' and 'simplemessageresponse' .. what they refere. How many such domain parts can be there and why ? My question is on not only for the client side, but also server side . SimpMessagingTemplate.convertAndSend("/topic/simplemessagesresponse", "Message to client");

There are tutorials showing the websocket server and client samples. But no enough details of rules to declare the subscriber path and how the subscriber path could be found.

What are the dependencies to change the path when it is declared in server and client side. I think another similar question is raised because of the a location change of a page where the websocket client is written.

Community
  • 1
  • 1
Débora
  • 5,816
  • 28
  • 99
  • 171

2 Answers2

1

Quoting the STOMP spec documentation:

Note that STOMP treats this destination as an opaque string and no delivery semantics are assumed by the name of a destination. You should consult your STOMP server's documentation to find out how to construct a destination name which gives you the delivery semantics that your application needs.

That means that destination semantics is broker specific:

Sergi Almar
  • 8,054
  • 3
  • 32
  • 30
1

I have implemented the websocket stomp by following this blog. I replaced @SendTo by SimpMessagingTemplate.

Here is my sample ChatController

@Autowired
private SimpMessagingTemplate simpMessagingTemplate;

@MessageMapping("/dualchart")
@ResponseBody
public void dualchat(MessageDTO message) {
    // forward message to destination
    String destination = "/topic/dualchat/" + message.getToUser();
    simpMessagingTemplate.convertAndSend(destination, message);
}

MessageDTO

@JsonIgnoreProperties
public class MessageDTO extends BaseModel {
    private String fromUser;
    private String toUser;
    private String message;
    public String getFromUser() {
        return fromUser;
    }
        public void setFromUser(String fromUser) {
        this.fromUser = fromUser;
    }
    public String getToUser() {
        return toUser;
    }
    public void setToUser(String toUser) {
        this.toUser = toUser;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}

Web Socket Config

<websocket:message-broker application-destination-prefix="/app"> 
    <websocket:stomp-endpoint path="/dualchat">
        <websocket:sockjs />
    </websocket:stomp-endpoint>
    <websocket:simple-broker prefix="/topic" /> 
</websocket:message-broker>

Javascript

var socket = new SockJS("/starter.web.admin/dualchat");
var stompClient = Stomp.over(page.socket);
stompClient.connect({}, socketJsConnectedCallback, socketJsErrorCallback);

function socketJsConnectedCallback() {
    var myId = "111"; // replace this Id
    stompClient.subscribe('/topic/dualchat/' + myId, function(message) {
        console.log("you reveived a message::::::::::" + JSON.stringify(message));
        // you have message, and you can do anything with it
    });
}
function socketJsErrorCallback(error){console.log(error);}

function sendMessage(message) {
    var data = {
        toUser : "1",
        message : message
    }
    stompClient.send("/app/dualchat", {}, JSON.stringify(data );
}

Hope this will help next search...

Ponleu
  • 1,492
  • 12
  • 27