CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-node-gcm

Easy interface for Google's Cloud Messaging service (now Firebase Cloud Messaging)

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

constants.mddocs/

Constants and Configuration

Configuration constants for FCM endpoints, timeouts, and error handling. These constants are used internally by the library but can be referenced for debugging and configuration purposes.

Capabilities

FCM Service Configuration

Core FCM service endpoints and connection settings.

const Constants = require('node-gcm').Constants;

/** FCM service endpoint hostname */
Constants.GCM_SEND_ENDPOINT; // 'fcm.googleapis.com'

/** FCM service endpoint path */
Constants.GCM_SEND_ENDPATH; // '/fcm/send'

/** Complete FCM service URL */
Constants.GCM_SEND_URI; // 'https://fcm.googleapis.com/fcm/send'

Timeout and Retry Configuration

Network timeout and retry behavior settings.

/** Socket timeout in milliseconds (3 minutes) */
Constants.SOCKET_TIMEOUT; // 180000

/** Initial delay for retry backoff in milliseconds */
Constants.BACKOFF_INITIAL_DELAY; // 1000

/** Maximum backoff delay in milliseconds */
Constants.MAX_BACKOFF_DELAY; // 1024000

Usage Example:

const gcm = require('node-gcm');

// Create sender with custom timeout based on constants
const sender = new gcm.Sender('YOUR_API_KEY', {
    timeout: gcm.Constants.SOCKET_TIMEOUT / 2  // Half the default timeout
});

// Use constants for custom retry logic
const customBackoff = gcm.Constants.BACKOFF_INITIAL_DELAY * 2;

Error Constants

FCM error codes for response handling and debugging.

/** Quota exceeded for sender */
Constants.ERROR_QUOTA_EXCEEDED; // 'QuotaExceeded'

/** Device quota exceeded */
Constants.ERROR_DEVICE_QUOTA_EXCEEDED; // 'DeviceQuotaExceeded'

/** Registration token missing from request */
Constants.ERROR_MISSING_REGISTRATION; // 'MissingRegistration'

/** Invalid registration token format */
Constants.ERROR_INVALID_REGISTRATION; // 'InvalidRegistration'

/** Registration token doesn't match sender ID */
Constants.ERROR_MISMATCH_SENDER_ID; // 'MismatchSenderId'

/** Registration token is no longer valid */
Constants.ERROR_NOT_REGISTERED; // 'NotRegistered'

/** Message payload exceeds size limit */
Constants.ERROR_MESSAGE_TOO_BIG; // 'MessageTooBig'

/** Collapse key missing for required message type */
Constants.ERROR_MISSING_COLLAPSE_KEY; // 'MissingCollapseKey'

/** FCM service temporarily unavailable */
Constants.ERROR_UNAVAILABLE; // 'Unavailable'

/** FCM internal server error */
Constants.ERROR_INTERNAL_SERVER_ERROR; // 'InternalServerError'

Usage Example:

const gcm = require('node-gcm');

sender.send(message, recipients, function(err, response) {
    if (response && response.results) {
        response.results.forEach((result, index) => {
            if (result.error) {
                switch (result.error) {
                    case gcm.Constants.ERROR_NOT_REGISTERED:
                        console.log(`Token ${index} is no longer registered`);
                        // Remove token from database
                        break;
                        
                    case gcm.Constants.ERROR_INVALID_REGISTRATION:
                        console.log(`Token ${index} has invalid format`);
                        // Remove invalid token
                        break;
                        
                    case gcm.Constants.ERROR_MESSAGE_TOO_BIG:
                        console.log('Message payload too large');
                        // Reduce message size
                        break;
                        
                    case gcm.Constants.ERROR_QUOTA_EXCEEDED:
                        console.log('Sending quota exceeded');
                        // Implement rate limiting
                        break;
                        
                    default:
                        console.log(`Unknown error: ${result.error}`);
                }
            }
        });
    }
});

Deprecated Constants (Legacy Support)

These constants are marked as deprecated but still available for backward compatibility.

// JSON field names - used internally for response parsing
Constants.TOKEN_MESSAGE_ID; // 'id'
Constants.TOKEN_CANONICAL_REG_ID; // 'registration_id' 
Constants.TOKEN_ERROR; // 'Error'
Constants.JSON_REGISTRATION_IDS; // 'registration_ids'
Constants.JSON_PAYLOAD; // 'data'
Constants.JSON_NOTIFICATION; // 'notification'
Constants.JSON_SUCCESS; // 'success'
Constants.JSON_FAILURE; // 'failure'
Constants.JSON_CANONICAL_IDS; // 'canonical_ids'
Constants.JSON_MULTICAST_ID; // 'multicast_id'
Constants.JSON_RESULTS; // 'results'
Constants.JSON_ERROR; // 'error'
Constants.JSON_MESSAGE_ID; // 'message_id'
Constants.UTF8; // 'UTF-8'

Constant Categories

Network Configuration

  • GCM_SEND_ENDPOINT - FCM hostname
  • GCM_SEND_ENDPATH - FCM API path
  • GCM_SEND_URI - Complete FCM URL
  • SOCKET_TIMEOUT - Request timeout

Retry Configuration

  • BACKOFF_INITIAL_DELAY - Initial retry delay
  • MAX_BACKOFF_DELAY - Maximum retry delay

Error Handling

  • ERROR_* constants - FCM error code strings
  • Used for response error matching and handling

Internal Protocol (Deprecated)

  • TOKEN_* and JSON_* constants - Response field names
  • Used internally for JSON parsing
  • Should not be used in application code

Usage Patterns

Error Handling with Constants

const { Sender, Message, Constants } = require('node-gcm');

function handleSendResponse(response, originalTokens) {
    const tokensToRemove = [];
    const tokensToUpdate = [];
    
    response.results.forEach((result, index) => {
        const token = originalTokens[index];
        
        if (result.error) {
            switch (result.error) {
                case Constants.ERROR_NOT_REGISTERED:
                case Constants.ERROR_INVALID_REGISTRATION:
                    tokensToRemove.push(token);
                    break;
                    
                case Constants.ERROR_UNAVAILABLE:
                case Constants.ERROR_INTERNAL_SERVER_ERROR:
                    // These are retried automatically by the library
                    console.log(`Temporary error for token ${token}`);
                    break;
            }
        } else if (result.registration_id) {
            tokensToUpdate.push({
                oldToken: token,
                newToken: result.registration_id
            });
        }
    });
    
    return { tokensToRemove, tokensToUpdate };
}

Custom Timeout Configuration

const sender = new Sender('YOUR_API_KEY', {
    // Use half the default socket timeout
    timeout: Constants.SOCKET_TIMEOUT / 2,
    
    // Custom retry configuration
    retries: 3,
    backoff: Constants.BACKOFF_INITIAL_DELAY * 1.5
});

Important Notes

  • Internal Usage: Most constants are used internally by the library
  • Error Matching: Use error constants for reliable error handling instead of hardcoded strings
  • Deprecated Constants: Avoid using deprecated TOKEN_* and JSON_* constants in new code
  • Timeout Values: All timeout values are in milliseconds
  • Backoff Strategy: Retry delays use exponential backoff between BACKOFF_INITIAL_DELAY and MAX_BACKOFF_DELAY
  • FCM Compatibility: Constants reflect current FCM API specifications

docs

constants.md

index.md

message.md

sender.md

tile.json