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.

Listen to messages

\f:(typescript:message.component.ts) // Subscribe messages\n this.db.messaging.messages()\n \t.subscribe((message: any) => console.log(message));\n\n // Subscribe to a topic\n this.db.messaging.topic(topic)\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}\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");\n

Send messages from client

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

Example



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