Server-side implementation of the MemberJunction Entity Communications Engine. Connects the entity/view system to the communication framework, enabling bulk messaging to records retrieved from entity views with full template rendering, related-entity data resolution, and multi-provider support.
graph TD
subgraph server["@memberjunction/entity-comm-server"]
ECE["EntityCommunicationsEngine\n(Singleton)"]
end
subgraph base["@memberjunction/entity-communications-base"]
ECB["EntityCommunicationsEngineBase"]
PARAMS["EntityCommunicationParams"]
end
subgraph comm["@memberjunction/communication-engine"]
CE["CommunicationEngine"]
end
subgraph core["@memberjunction/core"]
RV["RunView"]
MD["Metadata"]
end
subgraph providers["Registered Providers"]
SG["SendGrid"]
GM["Gmail"]
TW["Twilio"]
MSP["MS Graph"]
end
ECB --> ECE
ECE -->|queries records| RV
ECE -->|sends messages| CE
CE --> SG
CE --> GM
CE --> TW
CE --> MSP
style server fill:#2d8659,stroke:#1a5c3a,color:#fff
style base fill:#2d6a9f,stroke:#1a4971,color:#fff
style comm fill:#7c5295,stroke:#563a6b,color:#fff
style core fill:#b8762f,stroke:#8a5722,color:#fff
style providers fill:#2d8659,stroke:#1a5c3a,color:#fff
npm install @memberjunction/entity-communications-server
import { EntityCommunicationsEngine } from '@memberjunction/entity-communications-server';
import { EntityCommunicationParams } from '@memberjunction/entity-communications-base';
const engine = EntityCommunicationsEngine.Instance;
await engine.Config(false, contextUser);
const params: EntityCommunicationParams = {
EntityID: 'entity-uuid',
RunViewParams: {
EntityName: 'Contacts',
ExtraFilter: 'Status = "Active"'
},
ProviderName: 'SendGrid',
ProviderMessageTypeName: 'Email',
Message: {
Subject: 'Welcome',
Body: 'Hello {{FirstName}}!',
HTMLBodyTemplate: htmlTemplate
},
PreviewOnly: false
};
const result = await engine.RunEntityCommunication(params);
if (result.Success) {
console.log(`Sent ${result.Results.length} messages`);
}
When templates reference related entities, the engine automatically fetches the related data in batch and populates each recipient's context:
const params: EntityCommunicationParams = {
EntityID: 'customer-entity-id',
RunViewParams: { EntityName: 'Customers', ViewName: 'Premium Customers' },
ProviderName: 'SendGrid',
ProviderMessageTypeName: 'Email',
Message: {
BodyTemplate: templateWithRelatedEntities
}
};
// Engine auto-fetches related orders for each customer
const result = await engine.RunEntityCommunication(params);
if (engine.EntitySupportsCommunication(entityID)) {
const messageTypes = engine.GetEntityCommunicationMessageTypes(entityID);
messageTypes.forEach(mt => {
console.log(`Type: ${mt.BaseMessageType}`);
mt.CommunicationFields.forEach(field => {
console.log(` Field: ${field.FieldName} (Priority: ${field.Priority})`);
});
});
}
sequenceDiagram
participant App as Application
participant ECE as EntityCommunicationsEngine
participant RV as RunView
participant CE as CommunicationEngine
participant P as Provider
App->>ECE: RunEntityCommunication(params)
ECE->>ECE: Validate entity, provider, message type
ECE->>RV: RunView(params.RunViewParams)
RV-->>ECE: Entity records[]
ECE->>ECE: Load related entity data (batch)
loop For each record
ECE->>ECE: Build message with record context
ECE->>CE: SendSingleMessage(provider, type, message)
CE->>P: SendSingleMessage(processedMessage)
P-->>CE: MessageResult
CE-->>ECE: MessageResult
end
ECE-->>App: EntityCommunicationResult
PreviewOnly: true)SendAt schedulingCommunicationRun and CommunicationLog entitiesFor an entity to support communications:
Entity Communication Message Types in the MJ metadataEntity Communication Fields to define recipient address fieldsTemplates support multiple parameter types:
When using related entity parameters, the engine automatically:
| Package | Purpose |
|---|---|
@memberjunction/entity-communications-base |
Shared types and base engine class |
@memberjunction/communication-engine |
CommunicationEngine for message delivery |
@memberjunction/core |
RunView, Metadata, UserInfo, EntityInfo |
@memberjunction/core-entities |
Entity type definitions |
@memberjunction/global |
Class registration |
npm run build # Compile TypeScript
npm start # Watch mode