postMessage method
Posts a message
to a named channel. Optionally a destinationServerId
can be provided, in which case the message is sent only to that specific
server within the cluster. If no destinationServerId
is provided, the
message is passed on to all servers in the cluster.
Implementation
void postMessage(
String channelName,
SerializableEntity message, {
bool global = false,
}) {
if (global) {
// Send to Redis
assert(
Serverpod.instance!.redisController != null,
'Redis needs to be enabled to use this method',
);
var data =
Serverpod.instance!.serializationManager.encodeWithType(message);
Serverpod.instance!.redisController!.publish(channelName, data);
} else {
// Handle internally in this server instance
var channel = _channels[channelName];
if (channel == null) return;
for (var callback in channel) {
callback(message);
}
}
}