2022-04-08 14:32:08 -07:00
import EventSource from 'eventsource' ;
2023-03-09 09:13:15 -08:00
import type {
IDataObject ,
ITriggerFunctions ,
INodeType ,
INodeTypeDescription ,
ITriggerResponse ,
} from 'n8n-workflow' ;
2023-01-27 03:22:44 -08:00
import { jsonParse } from 'n8n-workflow' ;
2019-12-31 17:03:46 -08:00
export class SseTrigger implements INodeType {
description : INodeTypeDescription = {
displayName : 'SSE Trigger' ,
name : 'sseTrigger' ,
icon : 'fa:cloud-download-alt' ,
2024-06-06 04:34:30 -07:00
iconColor : 'dark-blue' ,
2019-12-31 17:03:46 -08:00
group : [ 'trigger' ] ,
version : 1 ,
2021-07-03 05:40:16 -07:00
description : 'Triggers the workflow when Server-Sent Events occur' ,
:sparkles: Improve Waiting Webhook call state in WF Canvas (#2430)
* N8N-2586 Improve Waiting Webhook call state in WF Canvas
* N8N-2586 Added watcher for showing Webhook's Node Tooltip on execution
* N8N-2586 Show helping tooltip for trigger node if wokrflow is running, it is a trigger node, if it is only one trigger node in WF
* N8N-2586 Rework/Move logic to computed property, Created getter for ActveTriggerNodesInWokrflow, Add style to trigger node's tooltip, remove comments
* N8N-2586 Added EventTriggerDescription prop in INodeTypeDescription Interface, Updated Logic for TriggerNode Tooltip based on the new prop
* N8N-2586 Add new use cases/watcher to show Trigger Nodes Tooltip / If has issues, if paused, if wokrlfow is running, Refactor Getter
* N8N-2586 Added z-index to tooltip, Added new Scenario for Tooltip if it is Draged&Droped on the WF
* N8N-2586 Refactor computed property for draged nodes
* N8N-2586 Fixed Conflicts
* N8N-2586 Fixed Tooltip
* N8N-2586 Dont show tooltip on core trigger nodes that execute automatically
* N8N-2586 Fixed Webhook tooltip when adding/deleting canvas during WF execution
* N8N-2586 Updated Logic, Simplify the code
* N8N-2586 Simplify Code
* N8N-2586 Added check for nodetype
* update dragging to use local state
* N8N-2586 Added eventTriggerDescription to Interval Node
* add comment, use new getter
* update to always check
Co-authored-by: Mutasem <mutdmour@gmail.com>
2021-11-25 14:33:41 -08:00
eventTriggerDescription : '' ,
2022-01-21 09:00:00 -08:00
activationMessage : 'You can now make calls to your SSE URL to trigger executions.' ,
2019-12-31 17:03:46 -08:00
defaults : {
name : 'SSE Trigger' ,
color : '#225577' ,
} ,
2023-06-23 03:29:24 -07:00
triggerPanel : {
header : '' ,
executionsHelp : {
inactive :
"<b>While building your workflow</b>, click the 'listen' button, then trigger an SSE event. This will trigger an execution, which will show up in this editor.<br /> <br /><b>Once you're happy with your workflow</b>, <a data-key='activate'>activate</a> it. Then every time a change is detected, the workflow will execute. These executions will show up in the <a data-key='executions'>executions list</a>, but not in the editor." ,
active :
"<b>While building your workflow</b>, click the 'listen' button, then trigger an SSE event. This will trigger an execution, which will show up in this editor.<br /> <br /><b>Your workflow will also execute automatically</b>, since it's activated. Every time a change is detected, this node will trigger an execution. These executions will show up in the <a data-key='executions'>executions list</a>, but not in the editor." ,
} ,
activationHint :
"Once you’ ve finished building your workflow, <a data-key='activate'>activate</a> it to have it also listen continuously (you just won’ t see those executions here)." ,
} ,
2019-12-31 17:03:46 -08:00
inputs : [ ] ,
outputs : [ 'main' ] ,
properties : [
{
displayName : 'URL' ,
name : 'url' ,
type : 'string' ,
default : '' ,
placeholder : 'http://example.com' ,
2022-05-06 14:01:25 -07:00
description : 'The URL to receive the SSE from' ,
2019-12-31 17:03:46 -08:00
required : true ,
} ,
2020-10-22 06:46:03 -07:00
] ,
2019-12-31 17:03:46 -08:00
} ;
async trigger ( this : ITriggerFunctions ) : Promise < ITriggerResponse > {
const url = this . getNodeParameter ( 'url' ) as string ;
const eventSource = new EventSource ( url ) ;
eventSource . onmessage = ( event ) = > {
2023-02-27 19:39:43 -08:00
const eventData = jsonParse < IDataObject > ( event . data as string , {
errorMessage : 'Invalid JSON for event data' ,
} ) ;
2019-12-31 17:03:46 -08:00
this . emit ( [ this . helpers . returnJsonArray ( [ eventData ] ) ] ) ;
} ;
async function closeFunction() {
eventSource . close ( ) ;
}
return {
closeFunction ,
} ;
}
}