Package Exports
- pushfire
Readme
Pushfire - Official Node.js Client
A product of Flywheel Studio
Official Node.js client for Pushfire - the powerful push notification and workflow automation platform.
Features
- 🚀 Easy device registration and management
- 👤 Subscriber authentication and profiling
- 🏷️ Tagging system for targeted notifications
- 📊 Workflow automation for engagement
- 🔒 Secure API communication
- 🌐 Cross-platform support (Browser, Node.js, React Native)
Installation
npm install pushfire
# or
yarn add pushfire
# or
pnpm add pushfireQuick Start
Client-side Usage
import { createClient } from 'pushfire';
// Initialize the client
const pushfire = createClient({
token: 'your_project_token',
});
// Register a device with FCM token
async function registerDevice(fcmToken) {
try {
const result = await pushfire.init(fcmToken);
console.log('Device registered:', result.deviceId);
console.log('Subscriber ID:', result.subscriberId);
} catch (error) {
console.error('Registration failed:', error);
}
}
// Login a subscriber
async function loginSubscriber(externalId, name, email) {
try {
const deviceId = pushfire.getStoredDeviceId();
if (!deviceId) {
throw new Error('Device not registered');
}
const result = await pushfire.subscribers.login({
deviceId,
externalId,
name,
email,
});
console.log('Subscriber logged in:', result.subscriberId);
} catch (error) {
console.error('Login failed:', error);
}
}Server-side Usage
import { createServer } from 'pushfire';
// Initialize the server
const pushfireServer = createServer({
token: 'your_project_token',
});
// Example in Express.js - Login subscriber with cookie context
app.post('/api/login', async (req, res) => {
try {
const cookieHeader = req.headers.cookie || '';
const { externalId, name, email } = req.body;
const { response, cookieHeaders } = await pushfireServer.loginSubscriberFromContext(
cookieHeader,
{
externalId,
name,
email,
}
);
// Set cookies in response
cookieHeaders.forEach(header => {
res.setHeader('Set-Cookie', header);
});
res.status(200).json({
success: true,
subscriberId: response.subscriberId,
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message,
});
}
});
// Add tag to subscriber using cookie context
app.post('/api/add-tag', async (req, res) => {
try {
const cookieHeader = req.headers.cookie || '';
const { tagId, value } = req.body;
await pushfireServer.addTagFromContext(cookieHeader, tagId, value);
res.status(200).json({ success: true });
} catch (error) {
res.status(500).json({
success: false,
error: error.message,
});
}
});API Reference
Client
createClient(config)- Creates a new Pushfire client instanceclient.init(fcmToken, options?)- Initializes Pushfire by registering the deviceclient.devices.register(data)- Registers a new deviceclient.devices.update(data)- Updates an existing deviceclient.subscribers.login(data)- Logs in a subscriberclient.subscribers.logout(data)- Logs out a subscriberclient.subscribers.update(data)- Updates subscriber informationclient.subscriberTags.add(data)- Adds a tag to a subscriberclient.subscriberTags.update(data)- Updates a subscriber tagclient.subscriberTags.remove(data)- Removes a tag from a subscriberclient.workflowExecutions.create(data)- Creates a workflow execution
Server
The server instance provides two types of functionality:
1. Direct Resource Access
All client resources are directly accessible through the server instance:
server.devices- Device operationsserver.subscribers- Subscriber operationsserver.subscriberTags- Tag operationsserver.workflowExecutions- Workflow operations
2. Context-Aware Methods
These methods use cookie headers to extract device and subscriber context:
server.extractContext(cookieHeader)- Extracts device context from cookiesserver.createCookieHeaders(data)- Creates Set-Cookie headers for device data
Device Operations:
server.registerDeviceWithContext(data)- Registers a device and returns cookie headersserver.updateDeviceFromContext(cookieHeader, data)- Updates a device using cookie context
Subscriber Operations:
server.loginSubscriberFromContext(cookieHeader, data)- Logs in a subscriber and returns cookie headersserver.logoutSubscriberFromContext(cookieHeader)- Logs out a subscriber and returns cookie headersserver.updateSubscriberFromContext(cookieHeader, data)- Updates a subscriber using cookie context
Tag Operations:
server.addTagFromContext(cookieHeader, tagId, value)- Adds a tag using cookie contextserver.updateTagFromContext(cookieHeader, tagId, value)- Updates a tag using cookie contextserver.removeTagFromContext(cookieHeader, tagId)- Removes a tag using cookie context
Workflow Operations:
server.createWorkflowExecutionFromContext(data)- Creates a workflow execution
MIT © Pushfire
Pushfire is a product of Flywheel Studio