Subscribing To Events

Subscribing to the Events for Keep

Keep is now using an MQTT event publisher MQTT instead of the previous SignalR SignalR.
The new API endpoint that works with MQTT in production is https://keepapi.feenicshosting.com. Customers hosted in Canada can use the https://ca.keepapi.feenicshosting.com address.

The endpoint for the MQTT service is returned when getting the SysInfo object from: https://keepapi.feenicshosting.com/api/sysinfo

MQTT

The following example is how you can connect to the MQTT Event service.

Please note that to use the example you need to have the MQTTnet and MQTTnet.Extensions.ManagedClient nugets available on the public nuget server. Nuget Sources and Packages

When a connection is made, the API checks the requested subscription for the provided token and the requested topic. The subscription is allowed if the provided token is allowed to access the requested instance. Only users with a valid access token from the API server can connect and subscribe. The access token carries who the user is, and to which instance the user logged in.

The allowed subscriptions topics are:

  • serverclock (Anyone can see server clock)
  • /INSTANCE_KEY/$ (subscribe to all events in that instance and shared/sub instances if an Enterprise or VAR instance.)
  • /INSTANCE_KEY/OBJECT_KEY (Subscribe to any events that the Object Key shows up in the object links)

For example, you can subscribe to Object Modified events in an Instance or you can subscribe to all events for a single person.

This Example will effectively subscribe you to all events in the scope of the provided instance key.

Example in C#

var eventpublisherUrl = (await client.GetSysInfo()).EventPublisherUrl;
var options = new ManagedMqttClientOptionsBuilder()
                .WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
                .WithClientOptions(new MqttClientOptionsBuilder()
                    .WithCommunicationTimeout(TimeSpan.FromSeconds(5)) 
					.WithKeepAlivePeriod(TimeSpan.FromSeconds(7.5))      
					.WithCredentials(client.TokenResponse.access_token, "")
                    .WithWebSocketServer(eventpublisherUrl+"/mqtt") 
                    .Build())
                .Build();
var eventsClient = new MqttFactory().CreateManagedMqttClient();
eventsClient.UseConnectedHandler(e =>
{
    Console.WriteLine($"Client connected {e}" );
});

eventsClient.UseDisconnectedHandler(e =>
{
    Console.WriteLine($"Client Disconnected.  WasConnected: {e.ClientWasConnected},");
});
eventsClient.UseApplicationMessageReceivedHandler(async e =>
{
	try
	{

		var message = BsonSerializer.Deserialize<EventMessageData>(e.ApplicationMessage.Payload);
		var data = System.Convert.FromBase64String(message.EventDataBsonBase64);
		Console.WriteLine($"MQTT:\t{message.Key}: {message.OccurredOn} | {message.MessageLong}");
	}catch(Exception ex)
	{
		Console.WriteLine($"IN MesagesRecievedHandler: \n{ex.ToString()}");
	}
});
await eventsClient.StartAsync(options);

await eventsClient.SubscribeAsync(new TopicFilterBuilder()
    .WithTopic($"/{currentInstance.Key}/$").Build());

This Example Shows how to use MQTT to subscribe to the posts made by the server containing its current server time.

Example in C#


eventsClient.UseApplicationMessageReceivedHandler(async e =>
{
	try
	{
		Console.WriteLine($"MQTT:\tSever Time: {new DateTime(BitConverter.ToInt64(e.ApplicationMessage.Payload, 0)}", ));
	}catch(Exception ex)
	{
		Console.WriteLine($"IN MesagesRecievedHandler: \n{ex.ToString()}");
	}
});
await eventsClient.StartAsync(options);

await eventsClient.SubscribeAsync(new TopicFilterBuilder()
    .WithTopic($"serverclock").Build());

Example in TypeScript

Check out our example on GitHub: https://github.com/feenicsinc/keep-MQTTExample