Skip to main content
Of course! This is an excellent and robust architecture for building a powerful, AI-driven enterprise application. Using Laravel for the core application logic, Vue for a dynamic front-end, and n8n as a decoupled workflow/AI engine is a modern and scalable approach. Your initial idea to use n8n webhooks as API endpoints is the right way to go. Let’s break down the implementation step-by-step, covering the integration, the database strategy, and the real-time chat interface.

High-Level Architecture

Here’s a visual representation of the flow we’re aiming for:

Part 1: Laravel & n8n Integration

Your plan to use a webhook is perfect. This decouples your systems: Laravel doesn’t need to know how the AI agent works, only where to send the data and how to get a response. 1. The n8n Workflow (The AI Agent) First, create your workflow in n8n.
  • Trigger Node: Webhook
  • This node generates a unique URL. This is the API endpoint your Laravel application will call.
  • It will automatically parse incoming JSON data (e.g., { "userId": 123, "conversationId": "xyz", "message": "Hello AI" }).
  • MongoDB Node: Get Conversation History
  • Use the conversationId from the webhook data to query your MongoDB conversations collection.
  • Fetch the last 5-10 messages to provide context to the AI.
  • AI Agent / LLM Node:
  • Use one of the built-in AI nodes (OpenAI, Hugging Face, Cohere, etc.).
  • Construct the prompt using the user’s new message and the conversation history you just fetched.
  • Execute the AI call. Many nodes support streaming.
  • MongoDB Node: Save AI Response
  • Take the response from the AI node.
  • Update the conversations collection in MongoDB, adding the AI’s message to the history.
  • (Optional) HTTP Request Node: Callback to Laravel
  • If you want n8n to proactively tell Laravel “I’m done,” you can add a final node that calls another API endpoint on your Laravel app. This is useful for fully asynchronous flows.
2. Laravel’s Role (The Orchestrator) Laravel will handle user authentication, manage the conversation state, and communicate with n8n. Create a Service or Action Class in Laravel: This encapsulates the logic for talking to n8n.
Secure Your n8n Webhook: In an enterprise app, you can’t have an open webhook.
  • Method 1: API Key: Add a static API key to the n8n webhook’s header authentication and send it from Laravel.
  • Method 2: Signed URLs (More Secure): Laravel can generate a temporary, signed URL that n8n can validate. This is more complex but more secure.

Part 2: Shared Database Strategy (MongoDB)

Using MongoDB as a shared state manager is a great idea. Suggested Schema for a conversations collection:
  • Laravel: Uses an Eloquent model (like jenssegers/laravel-mongodb) to create, read, and append messages.
  • n8n: Uses its built-in MongoDB node to read the messages array for context and append the new AI response. This keeps the full conversation history in one place.

Part 3: Vue.js Chat Interface (SSE vs. PrismPHP)

This is the crucial part for user experience. You want to show the AI response as it’s being generated (the “typing” effect). This requires streaming. SSE is a simple, efficient, and native browser technology for one-way communication from server to client. It’s perfect for streaming AI responses. How it works:
  1. Vue.js makes a single API request to a Laravel endpoint.
  2. This connection stays open.
  3. Laravel calls n8n.
  4. n8n starts its process. As the AI model generates tokens (words), n8n can use its streaming capabilities to send these tokens back to Laravel (e.g., via a second “streaming” webhook call).
  5. Laravel receives these tokens and immediately pushes them down the open SSE connection to Vue.
Implementation: Laravel Controller:
Vue.js Component:

Option 2: PrismPHP (or WebSockets via Laravel Reverb/Soketi)

PrismPHP is a high-level library that uses WebSockets (via Laravel Reverb or Soketi) to provide real-time features. When to use PrismPHP/WebSockets:
  • You need bi-directional communication (e.g., server needs to push alerts to the client without a prior request).
  • You need features like “user is typing…” indicators, online presence, or multi-user collaboration.
  • You are already using WebSockets for other features in your app.
How it would work:
  1. Setup: You’d need to install and configure Laravel Reverb (or Soketi) and Laravel Echo on the front end.
  2. Vue.js: The client connects to a WebSocket channel (e.g., private-chat.${userId}).
  3. Laravel: When the user sends a message, Laravel calls the n8n webhook.
  4. n8n -> Laravel Callback: The n8n workflow, upon receiving tokens from the AI, calls a callback API endpoint on Laravel.
  5. Laravel Event: This callback endpoint dispatches a Laravel event (e.g., AiTokenReceived).
  6. Broadcast: This event is broadcasted over the WebSocket channel.
  7. Vue.js Listener: Laravel Echo listens for this event on the front end and updates the UI.
This is more powerful but also significantly more complex to set up (managing a WebSocket server, broadcasting, events, etc.).

Recommendation: SSE vs. PrismPHP

For your specific use case of an AI chat interface, start with SSE.
  • Simplicity: It’s much easier to implement, requires no extra server daemons (like Reverb), and is highly efficient for this exact task.
  • Sufficiency: It perfectly solves the problem of streaming text from the server to the client.
If your application later evolves to require features like multi-user chat rooms, live collaboration, or real-time notifications completely unrelated to the user’s current action, then migrating to or adding WebSockets with a library like PrismPHP would be a logical next step.

Chatbox Implementation

Here is the visualization of the chat interface, the Vue 2 implementation, and how to handle advanced features like interactivity and multimodal uploads.

1. Text-Based Wireframe of the Chat Interface

This is a standard layout for an embedded enterprise chat agent, often placed in a drawer, a modal, or a dedicated page.

2. Vue 2 Implementation (Options API)

Here is how you implement the streaming chat interface using Vue 2 and the Options API. Prerequisites: You will likely want a library to render Markdown, as the AI will often return formatted text (bold, lists, code blocks). marked is a popular choice. npm install marked

3. Can the Chat Interface Be Interactive Like You (ChatGPT)?

Yes, absolutely. The architecture you’ve designed (Shared MongoDB + n8n LLM) already provides the foundation for this. Here is what makes the experience “interactive” and how your setup achieves it:
  1. Context Memory (Crucial):
  • ChatGPT remembers what you said 5 minutes ago.
  • Your solution: You are using MongoDB to store the conversations collection. Before n8n calls the AI, it must read the previous messages from MongoDB and include them in the prompt sent to the LLM. This is the key to interactivity.
  1. Streaming Responses:
  • You see the words appear as they are generated, which feels alive.
  • Your solution: Using SSE (Server-Sent Events) as demonstrated above provides this exact experience.
  1. Rich Formatting:
  • ChatGPT returns code blocks, lists, bold text, and tables.
  • Your solution: The LLM (e.g., OpenAI) will return Markdown by default. By using a library like marked in your Vue component (as shown in the code above), you convert that Markdown into HTML, giving you the same rich output.
  1. Capability Access (Tools):
  • Being “interactive” in an enterprise context means the AI can do things, not just talk.
  • Your solution: This is where n8n shines. You can give your AI agent “tools.”
  • User asks: “Check the status of order #123.”
  • n8n Workflow: The AI Agent node decides it needs to use the “Check Order Tool.” n8n then executes a Postgres/MySQL node (connecting to your Laravel DB) or an HTTP Request node (calling your internal ERP API) to get the data and feed it back to the AI to generate the answer.

4. How to Implement Multimodal Prompts (Uploading Images)

Handling image uploads with SSE requires a two-step process because EventSource (SSE) only supports GET requests, and you cannot upload a file body in a GET request.

The Process Flow

  1. User Selects Image: In Vue, the user clicks the paperclip and selects an image.
  2. Vue Uploads Image: Vue immediately sends a standard POST request (using Axios, NOT EventSource) to a Laravel endpoint, uploading the file.
  3. Laravel Saves Image: Laravel receives the file, validates it, and stores it (e.g., in storage/app/public or on AWS S3).
  4. Laravel Returns URL: Laravel responds to Vue with the public URL of the saved image.
  5. Vue Initiates Chat: Vue now initiates the SSE connection, passing both the user’s text message AND the image URL as query parameters.
  6. Laravel -> n8n: Laravel passes both the text and the image URL to the n8n webhook.
  7. n8n AI Agent: The n8n AI node (must use a model that supports vision, like gpt-4o, gpt-4-turbo, or claude-3-opus) receives the prompt and the image URL and processes both.

Laravel Implementation

Route:
Controller:

n8n Implementation

In n8n, ensure your Webhook trigger is set to receive the imageUrl in the JSON body or query parameters. Then, map that imageUrl into the “Image” or “Attachment” field of your AI Model node.