Send Messages¶
This guide covers sending and receiving encrypted messages, working with multiple topics, and subscribing to real-time updates.
Basic send and receive¶
Messages are encrypted client-side before sending. The server stores only ciphertext.
from reeeductio import Space
space = Space(
space_id='S...',
member_id='U...',
private_key=...,
symmetric_root=...,
base_url='http://localhost:8000',
)
# Send
result = space.post_encrypted_message(
topic_id='general',
msg_type='chat.text',
data=b'Hello, encrypted world!',
)
print('Posted:', result.message_hash)
# Receive
msgs = space.get_messages('general')
for msg in msgs:
text = space.decrypt_message_data(msg, 'general')
print(f'[{msg.message_hash[:12]}] {text.decode()}')
import { Space, stringToBytes, bytesToString } from 'reeeductio';
const space = new Space({ spaceId, keyPair, symmetricRoot, baseUrl: 'http://localhost:8000' });
// Send
const result = await space.postEncryptedMessage('general', 'chat.text', stringToBytes('Hello!'));
console.log('Posted:', result.message_hash);
// Receive
const { messages } = await space.getMessages('general');
for (const msg of messages) {
console.log(`[${msg.message_hash.slice(0, 12)}] ${bytesToString(space.decryptMessageData(msg, 'general'))}`);
}
Message types¶
The msg_type / type field is a free-form string you define. Use it to distinguish different kinds of events in a topic:
| Type | Meaning |
|---|---|
chat.text |
Plain text message |
chat.image |
Image (data is a blob ID) |
event.join |
User joined the space |
event.leave |
User left |
file.upload |
File uploaded (data is a blob ID) |
Working with multiple topics¶
Topics are independent — create as many as you like. Topic IDs must be lowercase alphanumeric + hyphens or underscores, 2–64 characters.
Querying messages¶
Filter messages by time range or limit the count:
Verifying the message chain¶
By default, the SDK verifies that prev_hash links are intact on every fetch. If the chain is broken, a ChainError is raised. You can also validate explicitly:
Real-time updates (WebSocket)¶
Subscribe to a topic to receive new messages as they arrive:
// WebSocket support coming soon — use polling in the meantime:
setInterval(async () => {
const { messages } = await space.getMessages('general', { from: lastSeen });
for (const msg of messages) {
console.log(bytesToString(space.decryptMessageData(msg, 'general')));
lastSeen = msg.server_timestamp;
}
}, 1000);
Attaching files¶
For large payloads, upload a blob and include its ID in the message:
Related¶
- Store Files — uploading and downloading blob attachments
- Topics & Messages — concept overview
- Access Control — controlling who can post