In BlazeDS you can use subtopics to filter messages on the server. A consumer (client) would then only receive messages based on the subtopic filter they subscribe to. This greatly reduces the load on clients and stops unwanted messages from being sent to clients.

In a recent project I needed a way for a client to change it's subtopic filter post subscribe. After some digging it was not as complicated as I thought it would be. The trick was to unsubscribe the client then have them resubscribe with the new filter.

Below is the code I am using to accomplish this. I am using the FDMS Bridge for this so, if you are using actionscript then adjust accordingly.

view plain print about
1function initPolling (sub){
2    var server = 'localhost';
3    var port = 80;
4    var cs = new ChannelSet();
5    
6    if (sub == '' || sub == undefined){
7        sub = '0';                    
8    }
9        
10    
11    cs.addChannel(new AMFChannel("longpolling-amf","http://"+server+":"+port+"/flex2gateway/cfamflongpolling"));                
12    
13    consumer = new Consumer();
14    consumer.setDestination('BasicQueue');
15    consumer.setChannelSet(cs);
16    consumer.setSubtopic("Basic.messaging.channel." + sub);
17    
18    // respond to messages from blazeDS
19    consumer.addEventListener("message", messageResponseHandler);
20    consumer.subscribe();
21}
22
23function messageResponseHandler(event){
24    alert(event.getMessage().getBody().MESSAGE);
25}
26
27function resub(){
28    // unsubscribe and then reinit with new subtopic
29    consumer.unsubscribe();
30    initPolling(999);
31}

Till next time..

--Dave