Learn how to use OKMQ in minutes with simple HTTP requests
First, sign up for an account and get your API token. You'll use this token to authenticate all your requests.
Let's create a queue for processing email notifications. This example shows how to set up a queue with automatic acknowledgment and a 5-minute TTL.
const host = 'https://api.okmq.net'
const token = process.env.OKMQ_TOKEN
// Create email notifications queue
const createQueue = async () => {
const response = await fetch(`${host}/queues/`, {
method: 'POST',
headers: {
authorization: `Bearer ${token}`,
'content-type': 'application/json',
},
body: JSON.stringify({
name: 'email-notifications',
auto_ack: true,
ttl: 300, // 5 minutes
}),
})
if (response.ok) {
console.log('Queue created successfully')
}
}
Now let's send an email notification message to the queue. Messages are sent as JSON arrays, allowing you to send multiple messages in a single request.
const sendEmailNotification = async (userEmail, subject, body) => {
const response = await fetch(`${host}/queues/email-notifications/messages`, {
method: 'POST',
headers: {
authorization: `Bearer ${token}`,
'content-type': 'application/json',
},
body: JSON.stringify([
{
id: `email-${Date.now()}`,
body: {
to: userEmail,
subject: subject,
body: body,
priority: 'normal'
}
}
]),
})
if (response.ok) {
console.log('Email notification queued')
}
}
// Send a welcome email notification
await sendEmailNotification(
'user@example.com',
'Welcome to our service!',
'Thank you for signing up...'
)
Create a worker process to continuously poll for new messages and process them. This example shows how to process email notifications.
const processEmailNotifications = async () => {
while (true) {
try {
const response = await fetch(`${host}/queues/email-notifications/messages`, {
headers: {
authorization: `Bearer ${token}`,
'content-type': 'application/json',
},
})
if (response.ok) {
const messages = await response.json()
for (const message of messages) {
console.log('Processing email:', message.id)
// Process the email notification
const emailData = message.body
await sendEmail(emailData.to, emailData.subject, emailData.body)
console.log(`Email sent to ${emailData.to}`)
}
}
} catch (error) {
console.error('Error processing messages:', error)
}
}
}
// Simulated email sending function
const sendEmail = async (to, subject, body) => {
// Your email sending logic here
console.log(`Sending email to ${to}: ${subject}`)
}
// Start processing
processEmailNotifications()
Here's a complete working example that demonstrates creating a queue, sending messages, and processing them:
const host = 'https://api.okmq.net'
const token = process.env.OKMQ_TOKEN
const main = async () => {
// Create the queue
await fetch(`${host}/queues/`, {
method: 'POST',
headers: {
authorization: `Bearer ${token}`,
'content-type': 'application/json',
},
body: JSON.stringify({
name: 'email-notifications',
auto_ack: true,
ttl: 300,
}),
})
// Start processing messages in the background
const processMessages = async () => {
while (true) {
const response = await fetch(`${host}/queues/email-notifications/messages`, {
headers: {
authorization: `Bearer ${token}`,
'content-type': 'application/json',
},
})
if (response.ok) {
const messages = await response.json()
for (const message of messages) {
console.log('Processing:', message.body.subject)
// Process your email here
}
}
}
}
// Start the background processor
processMessages()
// Send some test messages
const sendMessage = async (to, subject) => {
await fetch(`${host}/queues/email-notifications/messages`, {
method: 'POST',
headers: {
authorization: `Bearer ${token}`,
'content-type': 'application/json',
},
body: JSON.stringify([{
id: `email-${Date.now()}`,
body: { to, subject, body: 'Test message' }
}]),
})
}
// Send test messages every 5 seconds
setInterval(() => {
sendMessage('user@example.com', `Test email ${new Date().toISOString()}`)
}, 5000)
}
main().catch(console.error)
Explore all queue features, retry strategies, and configuration options
Message structure, tags, acknowledgments, and scheduled delivery