Quick Start
Get up and running with CloudSignal MQTT in 5 minutes
Quick Start
This guide will get you sending and receiving messages in under 5 minutes.
Basic Setup
import { CloudSignalClient } from '@cloudsignal/mqtt-client';
const client = new CloudSignalClient({
organizationId: 'your-org-id',
debug: true // Enable console logging
});
Connect
Before publishing or subscribing, establish a connection:
await client.connect();
console.log('Connected to CloudSignal!');
Subscribe to a Topic
client.subscribe('my/topic', (message, topic) => {
console.log(`Received on ${topic}:`, message);
});
Publish a Message
client.publish('my/topic', {
text: 'Hello from CloudSignal!',
timestamp: Date.now()
});
Complete Example
import { CloudSignalClient } from '@cloudsignal/mqtt-client';
async function main() {
const client = new CloudSignalClient({
organizationId: 'your-org-id'
});
// Handle connection events
client.on('connected', () => console.log('Connected'));
client.on('disconnected', () => console.log('Disconnected'));
client.on('error', (err) => console.error('Error:', err));
// Connect
await client.connect();
// Subscribe
client.subscribe('chat/messages', (message) => {
console.log('New message:', message);
});
// Publish
client.publish('chat/messages', {
user: 'Alice',
text: 'Hello everyone!'
});
}
main();
Browser Example (CDN)
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.cloudsignal.io/cloudsignal-mqtt.v2.1.0.js"></script>
</head>
<body>
<script>
const client = new CloudSignal({
organizationId: 'your-org-id'
});
client.connect().then(() => {
client.subscribe('notifications', (msg) => {
alert('Notification: ' + msg.text);
});
});
</script>
</body>
</html>
Next Steps
- Token Authentication - Secure your connections
- API Reference - Full client configuration options
- React Example - Integrate with React applications