How to dynamically create a new a Topic using Events Push plugin - events

To create a topic for an event I need to declare this in my conf/MyEvents.groovy file as follows:
events = {
"topicName" browser: true
}
I am wanting to use the server push for two things, pushing chat messages to a client and also for pushing notifications to a client.
Using the former as an example, I will need to create a new Topic for each conversation that is instantiated in the chat system at runtime, so that messages can be pushed to each of the conversation participants, so along the lines of
new Event(topic:'anotherTopicName',...)
which will allow me to call from a service :
import grails.events.*
class MyService {
def doSomething(){
...
event(topic:'anotherNewTopic', data:data)
}
}
Is there a method that will allow me to create a new Event topic? Or is there another way to implement this using Events Push

I've just done something similar. I needed to show some notifications based on the user that had logged in, so I set this in MyEvents.groovy:
events = {
'newNotification_*' browser:true
}
And when I need to send the notification:
event topic:"newNotification_${userId}",data:n
Then in my browser I can listen to those notifications with something similar to this:
grailsEvents.on("newNotification_"+myUser,function(data){

Related

Laravel best practice sending e-mail through API using templates

As you know many e-mail providers have the feature to use e-mail templates. Through their API you can send e-mails, just by setting the template id and passing through the variables. The predefined template will be send to the user.
For my project I want to use Mailjet API for transactional e-mails. What is the best way to call their email API in my project. Because laravel already has lots of email and notification features so I am looking for the best practice to integrate this in my project.
For example I want so make use of notifications. This notification has to call the mailjet email api and pass through the template id and the needed variables. What is the best way to archive this. With a custom notification channel maybe?
Or are there other good alternatives?
You are right, you can use an alternative mailing service while using Laravel's neat notifications feature. You will need to create a custom notification channel where you can define sending logic by hitting Mailjet's API endpoints. When the notification channel is all set up, you can specify it inside a notification itself where you can define the exact request body.
To make things easier, you may use Mailjet's official PHP wrapper so you don't have to work with raw HTTP requests.
Personally I used the same technique for Twilio SMS and Nexmo (Vonage) SMS Verification codes. I will add some of my code so you can see how I organised things:
class PhoneChannel
{
public function send($notifiable, Notification $notification)
{
if ($notification->resend) {
$this->repeatCall();
} else {
$this->initCall();
}
}
...
class VerifyPhone extends Notification
{
use Queueable;
public bool $resend;
public function __construct(bool $resend)
{
$this->resend = $resend;
}
public function via($notifiable): array
{
return [PhoneChannel::class];
}
}
The point is that you can define a request body inside the notification itself in order to access it later in the notification channel. Just add a method called toMailjet inside a notification, specify your variables, and then access it inside a channel via $notification->toMailjet()

How to configure MassTransit Saga to use several custom named topics?

I have a Saga that receives a command from the queue, sends a command to another queue, transitions to another state and waits for an event from the topic the name of which is custom (not named by MassTransit as an event type name). I am having a problem that Saga doesn't react to the event from custom named topic.
The Saga does the following:
Receive a command from the queue.
cfg.ReceiveEndpoint("submit-order-queue", configureEndpoint =>
{
configureEndpoint.ConfigureSaga<OrderShipmentState>(context);
configureEndpoint.RequiresSession = true;
});
Send a command to another queue and transition to another state. I am using a custom activity.
Consume an event from a topic. The topic name needs to be specified.
cfg.SubscriptionEndpoint(
"subsctiption1", "custom-name-topic",
configure =>
{
configure.ConfigureSaga<OrderShipmentState>(context);
});
I found a couple of similar questions like this with an answer from Chris Patterson
If you want to use a subscription endpoint for a saga, you would need
to configure the saga on the corresponding topic for each event.
I tried to achieve this with the code above but I haven't figured out how to specify the event in the configuration above. The method below will not work for me as I need to specify the custom topic name:
void SubscriptionEndpoint<T>(string subscriptionName, Action<IServiceBusSubscriptionEndpointConfigurator> configure) where T : class;
In the result, I am able to execute the 1st and 2nd steps, but on the 3rd step I get an event in the ASB subscription but the Saga doesn't react to that event.
Any suggestions what I am doing wrong?
In the configuration above, the receive endpoint will create subscriptions for each event handled by the saga and those subscriptions will forward messages sent to the topic to the submit-order-queue. You don't need to create the subscription endpoints.

Specifying queue name for Laravel notifications

I'm building a SAAS and I want each tenant to have their own queue for notifications. I have a notification class that implements Illuminate\Contracts\Queue\ShouldQueue and I send the notification like this
$user->notify($notification);
But I haven't found a way to specify the queue that I want the notification to be pushed to. I know that jobs can be pushed to specific queues with onQueue:
ProcessPodcast::dispatch($podcast)->onQueue('tenant1');
But is it possible to do something like this for queueable notifications as well?
Since your notification should use the Illuminate\Bus\Queueable trait you can simply set the $queue property of the object. There's a helper function for it:
$notification->onQueue('tenant1');
$user->notify($notification);

Spring STOMP - immediate response

I am implementing an Angular client that connects to a spring boot backend via STOMP.
When a client connects and wants to create a "business-group", said group is created in the backend and gets a UUID; and from that moment on other clients should be able to send to and receive messages from that group.
So I am creating a topic with said business-group id in the destination - something like
#MessageMapping("/foo/group/{id}")
However I want the creator of the group to immediately receive the business group id to be able to subscribe to it himself as well. And to be able to share the id with others (user to user).
I have used raw sockets for this before, so I was able to just use the connected user's session and then send back the id to him after the had sent the create-message. But since the session handling was business-group-ID based (so that only the users in a specific group receive the messages from that group), the whole dividing user sessions by business-group-ID had to be done manually and I was looking to upgrade this to something that does that handling for me.
However I am not sure how to achieve this with spring stomp. So my question is, is there a way for the creator of such a group to immediately receive the id as an answer/response to his initial "request"?
Because he can't subscribe to anything before he receives the group id.
Background:
It's basically like a chat app where a user can create a group/chatroom and then share it with others (via URL - which contains the ID); everyone that subscribes to it can then receive msgs from it and send msgs into it. But to do so the creator first needs the created ID.
I found a solution. This can be done via #SendToUser
#MessageMapping("/group.create")
#SendToUser(value="/topic/group.create", broadcast=false)
public Response createGroup(#Payload Message message) {
...
return createResponseWithId();
}
On the other end you only need to subscribe to '/user/topic/group.create'.

Glympse API - Handle Send Ticket Operation

I want to allow the application to call some method when the User Actually sends the Glympse Ticket
In simple terms I just want to get that is there any method to know whether the user has sent the Glympse after opening the Wizard or he/she has cancelled the wizard.
I have to do some operations when the user selects send Glympse Button
Looking forward for your valuable reply.
Thanks
The easiest way to call a method when the user has sent a Glympse is to register an event listener with the Glympse platform.
Here is an excerpt from the Glympse Send Lite demo showing how to do this:
// Start up the Glympse API
GlympseLiteWrapper.instance().start(this);
// Register this class as a listener for Glympse events
GlympseLiteWrapper.instance().getGlympse().addListener(this);
The parameter passed to addListener must be a class that implements GListenerLite. As a class using this interface, you will need to have an eventsOccured method.
#Override public void eventsOccurred(GGlympseLite glympse, int event, Object param1, Object param2)
{
if (0 != (event & LC.EVENT_TICKET_CREATED))
{
Log.d("", "Ticket created");
onTicketCreated((GTicketLite) param1);
}
}
The LC.EVENT_TICKET_CREATED event sounds like what you are interested in capturing. You can create a method such as onTicketCreated() to do things in response to a ticket being created.

Resources