Spring Websocket doesn't listen to Unsubscribe event with #stomp/stompjs - spring-websocket

I have a spring websocket project and created a very basic page to test it and everything seems to work fine, with the exception of the listener for the UNSUBSCRIBE message.
#EventListener
public void handleSessionSubscribeEvent(SessionSubscribeEvent event) {
System.out.println("SUBSCRIBED " + event);
}
#EventListener
public void handleSessionUnsubscribeEvent(SessionUnsubscribeEvent event) {
System.out.println("UNSUBSCRIBED " + event);
}
#EventListener
public void handleSessionDisconnectEvent(SessionDisconnectEvent event) {
System.out.println("DISCONNECTED " + event);
}
When the frontend makes the subscription, I get the first print from the listener, but when it unsubscribes the listener is not triggered.
You can see on Chrome's websocket log that the UNSUBSCRIBE message was sent:
["CONNECTED\nversion:1.1\nheart-beat:0,0\nuser-name:BsFJQRRyrKR4iUdSZyx0\n\n\u0000"]
["SUBSCRIBE\nid:666\ndestination:/topic/myTopic\n\n\u0000"]
["MESSAGE\ndestination:/topic/myTopic\ncontent-type:application/json\nsubscription:666\nmessage-id:hfxgxgci-4\ncontent-length:57\n\n{\"timestamp\":\"2021-08-04T14:34:22.157688Z\",\"value\":239.0}\u0000"]
["MESSAGE\ndestination:/topic/myTopic\ncontent-type:application/json\nsubscription:666\nmessage-id:hfxgxgci-4\ncontent-length:57\n\n{\"timestamp\":\"2021-08-04T14:34:22.157688Z\",\"value\":474.0}\u0000"]
["MESSAGE\ndestination:/topic/myTopic\ncontent-type:application/json\nsubscription:666\nmessage-id:hfxgxgci-4\ncontent-length:57\n\n{\"timestamp\":\"2021-08-04T14:34:22.157688Z\",\"value\":120.0}\u0000"]
["UNSUBSCRIBE\nid:666\n\n\u0000"]
This is my code in the frontend for the subscription:
const subscription = await WebSocketService.subscribe(`/topic/myTopic`);
An this is my code for the unsubscription:
return () => {
batcher.clear();
subscriptions.forEach((subscription) => {
subscription.unsubscribe();
});
};
As you can see from the Chrome log, the UNSUBSCRIBE message is being sent, but my listener doesn't trigger.
Any ideas ?

Related

"Unable to establish connection to Twilio Sync service" error

I initialized my twilio client in the code below but I am getting an error
Unable to establish connection to Twilio Sync service
My code:
public void build(String token, TaskCompletionListener<ChatClient, String> listener) {
ChatClient.Properties props =
new ChatClient.Properties.Builder()
.setRegion("us1")
.createProperties();
this.buildListener = listener;
//Toast.makeText(context,"++-==++",Toast.LENGTH_LONG).show();
ChatClient.create(context.getApplicationContext(),
token,
props,
new CallbackListener<ChatClient>()
{
#Override
public void onSuccess(ChatClient chatClient) {
Toast.makeText(context,chatClient.getMyIdentity(),Toast.LENGTH_LONG).show();
}
#Override
public void onError (ErrorInfo errorInfo)
{
Log.e("++==00",errorInfo.getMessage());
Toast.makeText(context,errorInfo.getMessage(),Toast.LENGTH_LONG).show();
}
});

SendAsync always showing Status Waiting using MVCmailer

I am developing application in MVC5 and MVCMailer. I am sending email using SendAsync command which always show Status = "WaitingforResponse" and mail is not sent. If I use "Send" command instead, it works fine. Here is my code:
var v = new UserMailer().SendCampaignMail("email_address").SendAsync();
and UserMailer.cs
public virtual MvcMailMessage SendCampaignMail(string Email)
{
try
{
return Populate(x =>
{
x.Subject = "Campaing";
x.ViewName = "PasswordReset";
x.To.Add(Email);
});
}
catch(Exception ex)
{
throw ex;
}
}
Screenshot for response: http://prntscr.com/82yfwq
Thanks for your Help:

Calling a Client Method on a Windows Service

I have a SignalR client in a Windows Service that successfully calls a Server method in an MVC app. First the Server Code:
public class AlphaHub : Hub
{
public void Hello(string message)
{
// We got the string from the Windows Service
// using SignalR. Now need to send to the clients
Clients.All.addNewMessageToPage(message);
// Send message to Windows Service
}
and
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
app.MapSignalR("/signalr", new HubConfiguration());
}
}
The Windows Service client is:
protected override async void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
try
{
var hubConnection = new HubConnection("http://localhost.com/signalr", useDefaultUrl: false);
IHubProxy alphaProxy = hubConnection.CreateHubProxy("AlphaHub");
await hubConnection.Start();
await alphaProxy.Invoke("Hello", "Message from Service");
}
catch (Exception ex)
{
eventLog1.WriteEntry(ex.Message);
}
}
It sends a message to the MVC Server. Now I want to call the other way from server to client. The Client Programming Guide has the following code examples which will NOT work as this is not a desktop.
WinRT Client code for method called from server without parameters (see WPF and Silverlight examples later in this topic)
var hubConnection = new HubConnection("http://www.contoso.com/");
IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("StockTickerHub");
stockTickerHub.On("notify", () =>
// Context is a reference to SynchronizationContext.Current
Context.Post(delegate
{
textBox.Text += "Notified!\n";
}, null)
);
await hubConnection.Start();
How can I call a method on the client?
The .NET client side code seems fine. You can simply get rid of Context.Post since your client is running inside of a Windows Service and doesn't need a SyncContext:
protected override async void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
try
{
var hubConnection = new HubConnection("http://localhost.com/signalr", useDefaultUrl: false);
IHubProxy alphaProxy = hubConnection.CreateHubProxy("AlphaHub");
stockTickerHub.On("Notify", () => eventLog1.WriteEntry("Notified!"));
await hubConnection.Start();
await alphaProxy.Invoke("Hello", "Message from Service");
}
catch (Exception ex)
{
eventLog1.WriteEntry(ex.Message);
}
}
You can invoke the "Notify" callback from inside your AlphaHub on the server like so:
public class AlphaHub : Hub
{
public void Hello(string message)
{
// We got the string from the Windows Service
// using SignalR. Now need to send to the clients
Clients.All.addNewMessageToPage(message);
// Send message to the Windows Service
Clients.All.Notify();
}
Any client will be able to listen to these notifications since we are using Clients.All. If you want to avoid this, you need some way to authenticate your Windows Service and get its ConnectionId. Once you have that, you can send to the Windows Service specifically like so:
Clients.Client(serviceConnectionId).Notify();
Hope this helps.
Windows Service with self hosted SignalR
public partial class MyWindowsService : ServiceBase
{
IDisposable SignalR { get; set; }
public class SignalRStartup
{
public static IAppBuilder App = null;
public void Configuration(IAppBuilder app)
{
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration()
{
// EnableDetailedErrors = true
};
map.RunSignalR(hubConfiguration);
});
}
}
public MyWindowsService()
{
InitializeComponent();
}
protected override void OnStart(string[] args) { Start(); }
protected override void OnStop() { Stop(); }
public void Start()
{
SignalR = WebApp.Start<SignalRStartup>("http://localhost:8085/signalr");
CallToMvcJavascript();
}
public new void Stop()
{
SignalR.Dispose();
}
private void CallToMvcJavascript(){
GlobalHost.ConnectionManager.GetHubContext<MyHub>().Clients.All.addNotice(// object/data to send//);
}
}
The Hub in the Windows Service
public class MyHub : Hub
{
public void Send()
{
Clients.All.confirmSend("The service received the client message");
}
}
The Javascript
$.connection.hub.logging = true;
$.connection.hub.url = "http://localhost:8085/signalr";
var notices = $.connection.myHub;
notices.client.addNotice = function(notice) {
console.log(notice);
};
notices.client.confirmSend = function(msg) {
alert(msg);
};
$.connection.hub.start().done(function() {
$('#myTestBtn').on('click', function() {
notices.server.send();
});
});

SMACK: how do I listen for user availability status changes?

How do I subscribe to listen to user availability status changes in SMACK?
To get the availability status for a user I use the following:
XMPPConnection.getRoster().getPresence(name).isAvailable();
But how can I subscribe so I receive some notifications whenever the status changes? (So I don't have to poll).
You set up a listener for Roster and Presence changes.
this code may help you :
roster.addRosterListener(new RosterListener() {
// Ignored events public void entriesAdded(Collection<String> addresses) {}
public void entriesDeleted(Collection<String> addresses) {}
public void entriesUpdated(Collection<String> addresses) {}
public void presenceChanged(Presence presence) {
System.out.println("Presence changed: " + presence.getFrom() + " " + presence);
}
#Override
public void entriesAdded(Collection<String> arg0) {
// TODO Auto-generated method stub
}
});
roster.addRosterListener(new RosterListener() {
// Ignored events public void entriesAdded(Collection<String> addresses) {}
public void entriesDeleted(Collection<String> addresses) {}
public void entriesUpdated(Collection<String> addresses) {}
public void presenceChanged(Presence presence) {
Log.e(TAG, presence.getStatus());
Log.e(TAG,presence.getFrom());
Log.e(TAG, presence.getLanguage());
Log.e(TAG,presence.getDefaultLanguage());
Log.e(TAG, presence.getType().toString());
}
#Override
public void entriesAdded(Collection<String> arg0) {
// TODO Auto-generated method stub
}
});

Runtime Exception in Send Message in BlackBerry

I am getting RunTime Exception when I am Running this code..Please Go Through it and Help me if you have any idea. Thanks..
private void sendSMS(String phone, String message) throws IOException
{
// TODO Auto-generated method stub
Dialog.alert("Hello..In Send SMS Function");
System.out.println("in send sms function");
MessageConnection conn =
(MessageConnection)Connector.open("sms://+919099956325");
TextMessage tmsg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
tmsg.setAddress("sms://+919429441335");
tmsg.setPayloadText("HIIiii");
System.out.println("Text message is>>"+tmsg);
conn.send(tmsg);
}
instead of
System.out.println("Text message is>>"+tmsg);
use
System.out.println("Text message is>>"+tmsg.getPayloadText());
Also Connector.open is a blocking operation and should not be called from a main event thread.
You have Dialog.alert which will only work on a event thread. Do this
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.alert("Hello..In Send SMS Function");
}
});
Try this code . this starts a new thread and calls sendsms method
new Thread(new Runnable() {
public void run() {
try {
sendSMS("123456789","message");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
private void sendSMS(String phone, String message) throws IOException
{
try {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.alert("Hello..In Send SMS Function");
}
});
System.out.println("in send sms function");
MessageConnection conn =
(MessageConnection)Connector.open("sms://+919099956325");
TextMessage tmsg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
tmsg.setAddress("sms://+919429441335");
tmsg.setPayloadText("HIIiii");
System.out.println("Text message is>>"+tmsg.getPayloadText());
conn.send(tmsg);
} catch (Exception e) {
System.out.println("Exception is >>"+e.toString());
}
}

Resources