mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
✨ Add SSE Trigger-Node
This commit is contained in:
57
packages/nodes-base/nodes/SseTrigger.node.ts
Normal file
57
packages/nodes-base/nodes/SseTrigger.node.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import * as EventSource from 'eventsource';
|
||||
import { ITriggerFunctions } from 'n8n-core';
|
||||
import {
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
ITriggerResponse,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
|
||||
export class SseTrigger implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'SSE Trigger',
|
||||
name: 'sseTrigger',
|
||||
icon: 'fa:cloud-download-alt',
|
||||
group: ['trigger'],
|
||||
version: 1,
|
||||
description: 'Triggers worklfow on a new Server-Sent Event',
|
||||
defaults: {
|
||||
name: 'SSE Trigger',
|
||||
color: '#225577',
|
||||
},
|
||||
inputs: [],
|
||||
outputs: ['main'],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'URL',
|
||||
name: 'url',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'http://example.com',
|
||||
description: 'The URL to receive the SSE from.',
|
||||
required: true,
|
||||
},
|
||||
]
|
||||
};
|
||||
|
||||
|
||||
async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
|
||||
const url = this.getNodeParameter('url') as string;
|
||||
|
||||
const eventSource = new EventSource(url);
|
||||
|
||||
eventSource.onmessage = (event) => {
|
||||
const eventData = JSON.parse(event.data);
|
||||
this.emit([this.helpers.returnJsonArray([eventData])]);
|
||||
};
|
||||
|
||||
async function closeFunction() {
|
||||
eventSource.close();
|
||||
}
|
||||
|
||||
return {
|
||||
closeFunction,
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user