Skip to content

Publishing Messages

To publish a message, choose the abstraction level that matches your application’s responsibility:

  • [Reactive]ValkeyConnection is the low-level contract and operates on already-serialized (byte[]) channel and message data.
  • [Reactive]ValkeyOperations (typically through ValkeyTemplate) publishes messages by using the template’s configured serializers for conversion.
  • ValkeyMessageSendingTemplate integrates with Spring Messaging and delegates payload conversion to a MessageConverter, selecting converters according to the payload type and, if present, the contentType header.

The imperative API offers all three variants. The reactive API offers the low-level connection and ReactiveValkeyOperations variants.

The following table summarizes the differences between the sending options:

VariantResponsibilityConversion model
[Reactive]ValkeyConnectionPublish binary channel and message data.Application code provides the binary (byte[]/ByteBuffer) values.
ValkeyTemplate / [Reactive]ValkeyOperationsPublish application values through the Valkey data access abstraction.Uses the configured value serializer for the message body. Publication is therefore bound to the template-specific value serializer.
ValkeyMessageSendingTemplatePublish through Spring’s Message<?> abstraction for flexible message conversion.Uses ValkeyMessageConverters to select a MessageConverter according to the payload type and, if present, the MessageHeaders.CONTENT_TYPE header. Useful for applications that already use Spring Messaging or annotation-driven listeners.

The following example shows the available sending variants:

// send message through connection
ValkeyConnection connection = …
byte[] channel = …
byte[] message = …
connection.pubSubCommands().publish(channel, message);
// send message through ValkeyOperations
ValkeyOperations<String, Object> operations = …
Long numberOfClients = operations.convertAndSend("chatroom", "hello!");

Note: All three drivers (Valkey GLIDE, Lettuce, and Jedis) support publishing messages via Pub/Sub.