SapphireDb logo SapphireDb

Messaging

Because SapphireDb already creates a bidirectional connection it is very easy to use it for general communication between client and server.
SapphireDb includes a messaging system that can be easily used.

Basic

Example

Listen to messages

\f:(typescript:Angular) // Subscribe messages\n this.db.messaging.messages()\n \t.subscribe((message: any) => console.log(message));

Send messages from server

You can use the SapphireMessageSender to publish messages from server to clients.
\f:(csharp:MessageActions.cs) public class MessageActions : ActionHandlerBase\n {\n \tprivate readonly SapphireMessageSender MessageSender;\n\n \tpublic MessageActions(SapphireMessageSender messageSender)\n \t{\n \t\tMessageSender = messageSender;\n \t}\n\n \tpublic void Send(string message)\n \t{\n \t\tMessageSender.Send(message);\n \t}\n\n \tpublic void SendToAdmin(string message)\n \t{\n \t\t// Filters connections by the given filter 'admin'. Configured in Startup.cs\n \t\tMessageSender.Send(message, "admin");\n \t} \f:(csharp:Startup.cs) services.AddSapphireDb(options)\n \t.AddContext<RealtimeContext>(cfg => cfg.UseFileContextDatabase(databaseName: "realtime"))\n \t.AddMessageFilter("admin", i => i.User.IsInRole("admin");\n

Send messages from client

It is also possible to send messages directly from clients to other clients.
\f:(typescript:Angular) // Send a message to all clients\n this.db.messaging.send(message);\n\n // Send a message to all clients that match filter 'admin'\n this.db.messaging.send(message, 'admin');

Topics

Example

Listen to messages

\f:(typescript:Angular) // Subscribe to a topic\n this.db.messaging.topic(topic)\n \t.subscribe((message: any) => console.log(message));
You can also use Glob-Patterns to subscribe topics.
For example: admin/** would match any topic starting with admin/

Send messages from server

You can use the SapphireMessageSender to publish messages from server to clients.
\f:(csharp:MessageActions.cs) public class MessageActions : ActionHandlerBase\n {\n \tprivate readonly SapphireMessageSender MessageSender;\n\n \tpublic MessageActions(SapphireMessageSender messageSender)\n \t{\n \t\tMessageSender = messageSender;\n \t}\n\n \tpublic void PublishToTopic(string message)\n \t{\n \t\tMessageSender.Publish("topic", message);\n \t}\n } \f:(csharp:Startup.cs) services.AddSapphireDb(options)\n \t.AddContext<RealtimeContext>(cfg => cfg.UseFileContextDatabase(databaseName: "realtime"))\n \t.AddMessageFilter("admin", i => i.User.IsInRole("admin");

Send messages from client

It is also possible to send messages directly from clients to other clients.
\f:(typescript:Angular) // Publish a message to a topic\n this.db.messaging.publish(topic, message);

Configure topic permissions

You can control the permission of a user in a topic by configuring it in Startup.cs.
\f:(csharp:Startup.cs) services.AddSapphireDb(options)\n \t.AddContext<RealtimeContext>(cfg => cfg.UseFileContextDatabase(databaseName: "realtime"))\n \t// The topic is only subscribable by users in role 'admin' and only users in role 'superuser' can publish things to topic\n \t.AddTopicConfiguration("admin", i => i.User.IsInRole("admin"), i => i.User.IsInRole("superuser"));
You can also use Glob-Patterns to define topic configuraitons.
For example: admin/** would match any topic starting with admin/

This website uses Google Analytics. If you want to disable it just unselect the option: