Server send events - triggers

Hi Guys,

I subscribe to triggers on the client side in a dynamic way where triggers constantly change, just want to know if there is a way to clear all triggers on the client without keeping track of all triggers by name. Similiar to a method like => Client.ClearAllTriggers()

Also, is there a way to prevent subscribing to the same trigger twice to prevent an event firing twice.

Thanks in advance.

Which client are you referring to?

Apologies, using C# client (UWP) with latest version of SS.

You should be able to clear all Handlers with:

client.Handlers.Clear();

And all Named Receivers with:

client.NamedReceivers.Clear();
client.ReceiverTypes.Clear();

Hi @mythz

All 3 solutions are not working.

Just for clarity, I’m referring to trigger events:

client.AddListener(“customEvent”, msg => { … });
client.RemoveListener(“customEvent”, handler);

Thanks

Ok the list of event listeners is private, so I’ve added new APIs you can use to remove all listeners in this commit:

client.RemoveAllListeners();

Or you can remove all registered Handlers, Named Receivers and Listeners with:

client.RemoveAllRegistrations();

This change is available from v5.0.3 that’s now available on MyGet.

Thanks @myth. I assume then that I will be able to loop through these listeners so that I can prevent subscribing to the same listener twice ?

Also, out of interest why the handler on client.RemoveListener(“customEvent”, handler);
and not just client.RemoveListener(“customEvent”);

Thanks again.

Because you can have multiple handlers registered to the same event, calling RemoveListener wont be able to know which handler to remove.

I’ve added additional APIs that should help in this commit:

client.RemoveListeners(eventName);     // remove all handlers registered to this event
client.HasListener(eventName,handler); // is this handler already registered
client.HasListeners(eventName);        // are any handlers registered for this event

This change is now on MyGet, if you already have v5.0.3 installed you’ll need to clear your NuGet cache with:

$ nuget locals all -clear

Awesome stuff, that sorts me out nicely :wink: Thanks again.

1 Like

Hi @myth,

Currently, when using a trigger say client.AddListener(“customEvent”, msg => { … });

When I shut down the server and keep the client open. After restarting the server and then sending an event trigger the trigger fires twice on the client. Checked it with 2 different implementations 1 mobile + 1 uwp. Therefor the SSE client connects again but seems like it adds another trigger without me doing anything I’m just letting SSE automatically reconnect.

Anything I am missing that I should be doing to prevent a double fire of events.

Are you saying your custom "customEvent" trigger event is fired when the Server Events reconnects? Because that’s unlikely since the SSE client doesn’t persist events and only triggers them as it receives it, i.e. it couldn’t do resend a past event even if it wanted to.

So all I can think of is that you’re seeing the result of double-registering for the event or in some hypothetical scenario since event triggers are synchronous that your handler is blocking the client from firing the other events and during that time the server event client reconnects, your handler then unblocks and continues firing the previous events, although that seems very unlikely but I don’t otherwise see how its possible.

If you can put together a small stand-alone example i can run locally to reproduce the issue I will be able to find out what’s really happening.

"Are you saying your custom “customEvent” trigger event is fired when the Server Events reconnects? "

No, I initiate another api call to fire the event again after the client reconnected with
this.ServerEvents.NotifyChannel(“Home”, “trigger.customEvent” , new obj(){})

Afther this rest call I then receive 2 fires on the client side.

Therefor, if I restarted the server say 5 times, I will receive 5 events being fired. I am simply initiating 1 api call to fire.

Then sounds like you have multiple registrations? If you’re dynamically registering them, are you clearing out any previous handlers registered before-hand?

Hi @myth,

Not to worry. I assumed the OnConnect will be fired once and thereafter the OnReconnect event. looks like the onconnect event also gets fires after reconnection and this is where i put in some listeners.

Thanks again.

1 Like

Hi @mythz

Been using the SSE listener in UWP without any problems. When using triggers in XF android the solution build in debug mode, but gives me the following error when running:

Also, changing droid project to release mode results in:

I cleared cache as per your docs and also delete bin/obj folders twice, same result. Running SS in .net standard class library in XF as opposed to main solution in UWP.

Any ideas ?

Thanks in advance.

These are the newer APIs so I’m assuming it’s because you’ve got a dirty version. Did you try clearing our your NuGet cache?

$ nuget locals all -clear

Hi @myth,

Yes, also tried that with existing retries. Any plans SS will issue official update to version soon, then i can just wait for official version.

No there’s no release in the immediate future.

BTW I can see ServerEventsClient.HasListeners() in the .NET 4.5 and .NET Standard 2.0 builds of the ServiceStack.Client and ServiceStack.Client.Core packages, so you’re likely referencing an old version somewhere.

thanks @mythz Will keep on trying.