Laravelvideo· Jun 7, 2025· 9:35· 24.1K views on YouTube
React at a moment you like. The best moments light up here.
At this point in time, especially as developers, we can't escape AI. So, it means we have to learn how to work with AI or better yet, how to make our applications work with what AI expects. And in most circumstances, especially when you're waiting for a response from an AI provider, you are being streamed that response or expecting to stream that response from the AI provider. So, how can we do this within Laravel? Well, I have something cool to show you. In
this video, I'll show you what streaming responses look like from a Laravel backend as well as introducing you to the use stream hook for React or Vue. We'll build a quick little demo in Vue, showing you how to use the Ustream hook with something like OpenAI. And then we'll take a closer look at a newly released Laravel official AI chat application demo for you to see how you might be using this at a at a bigger scale. First off, the Laravel backend
side. How do we stream responses to our client? Whether we're using live wire or whether we're using something like inertia with react and view or even a single page application. How do we stream something from a response? Well, Laravel makes it easy with this return response stream. And while this isn't specific for AI applications, it does work great for that use case because usually you're given a response back from an AI provider that is quite large
most of the time. Streaming it in allows your client to start processing that data before it's actually finished sending it. And here in the docs that you can find linked below, you can see exactly what's happening here. A bunch of fancy stuff that Laravel is doing in the back end to give you a generator as well as disabling engine X output buffering. So the meat and potatoes of this conversation comes to how can I then consume that streamed response that Laravel is sending out to my client. Well, if you're using React or Vue,
there is a stream-- view or a stream- react package officially from Laravel to help you make that easier. Of course, the docs give you an example. And yes, this isn't just for AI chat applications, but it can be used for anything that you might be streaming from the server, but why don't we build that AI chat app in Vue before we dive into the full LRA chat demo. In order to speed things along, I've scaffolded a Vue starter kit. I've installed the
Prism AI package which allows us to connect to different AI providers. I'm just using an OpenAI key that I already have set up in my application. So following the docs, there's a couple of things that we need to do on the server in order to stream that information to wherever we're capturing it from. So first off, I'm going to say that we have a route that is chat. We're going to render an inertia page and that's our chat page. We'll create that here in a
second. And then we have a post route that is chat. And here is where we have that response stream. Now one thing to note is this is going to be a little bit different than most maybe like post requests that you are used to writing within a full stack Laravel inertia application. Why? Because you need to kind of think of this like as an API route where it's not going to be specifically connected because we are needing that initial connection. We're not going to use an inertia use form
helper. We're going to use the use stream hook to send information and receive information from there. Kind of like a two-way connection. Because of that, we do need to set these cache controls, content types, and this Excel buffering as well. And since I'm using Prism, it makes it really easy to say in the response, I want to use OpenAI with the GPT4.1 nano model. And then I'm just sending the messages that we're sending
along to this particular request. Finally, for each response, we'll chunk that up and flush it out. Now, why don't we go ahead and create that chat view page. So, I'm just going to create a new page, chat.view. We'll paste everything in. The gist for this view simple application is going to be in the description as well as the full GitHub for the Larhat application that we'll dive into in a little bit that is written in React. And here's the thing to note. We're going to be using the Laravel stream view hook. And like I
mentioned, all the requests to the servers is going to be contained in that one little hook. So we have a HTML form element that we're grabbing all the data. We're adding it on and pushing it together. That's how we get the continuous chat, but then we are sending that message here. Of course, I also added a streaming indicator just to have a little dot saying that yes, this is happening the way we want it to happen. But the majority of the magic that's really happening behind the scenes is in
this use stream hook. The rest is just UI syntax and a little magic on top of it to say uh I want to do something with the events and stream that I'm receiving. But I also want to do something with the things that I'm sending. This stream hook again is the thing that ties this all together because we have the ability to send data have that send hook that send function in that hook to send information. say that hey if I press the escape key we
want to cancel and then some nicities like is this streaming and what specific ID is being sent with that stream so going to that chat route here we have it we might actually look into the console and see what's happening within the network so I'm going to say hi there and we get a response immediately back well what's happening right here we have this chat request within event stream this is a browser standard to say okay I am
streaming that information back to this particular page and you can see that response just in plain text right there. So yes, the majority of that magic is all going to live in that stream hook to be able to not have to worry about axios requests or the like. One thing to note is that you will have to make sure you are sending a CSRF token because the Ustream hook does send and receive data for you. You're not using something within inertia. And while this is pointed out in the Laravel docs for the
streaming response, you just have to make sure that your meta tag for the CSR CSRF token in your app.php exists is there. So what does this look like for a more fullfeatured application such as Larhat that I'm going to demo for you right now where we have some advanced features that you might want to think about if you are building an AI chat application. While we're not going to dive into every single element of this application, I do
want to show you some neat things that I found. Since in the initial demo, we weren't actually saving those chats, those conversations uh to any database. Here, we're saying that the user has the ability to create chats. And then every single message that is being created is not only uh being created in in that chat, but is also any streamed response is then also attached to that chat as well. So if we refresh the page, we see all the existing conversation. Same
thing with the existing messages as well. We're grabbing all the existing messages that exist in the chat, but then we are also using those as context for the API, the AI's streamed response. Now, in this demo, I'm not using Prism. I'm only using the Open AI Laravel package, which is a great option as well. Now, here's one neat thing I want to show you. The event stream. Now this is not the same as a stream in the sense that uh this is kind of like a two-way conversation. The event stream is
happening on the server and we want to send it to the client. Now what am I using this for? To update a title. So when the first chat happens uh well we can tell OpenAI say hey this is the chat information. Give me a title and then on the server we can just send that back to the client update the title the conversation of the chat. This is a great use case for event streaming, something that happens on the server, similar to like broadcasting, but you do
want to stream it into the client. One thing to note, if you are using the event streaming, it's going to try to access that information even if that information has already been streamed down. So you do need to uh conditionally show that particular component where in this case um if only if there should be a title being generated is when I'm showing that title generator component that is watching waiting for that stream from the event server. And then while there's a bunch of different ways to do
this, I found that the best way was having a separate API request to get all the list of chats for a user. So that way I can show it in the sidebar and I didn't have to use it as particular props in a page or even the template layout. So what does this look like? Well, you can try it out yourself at larat.laravel.cloud. And this is what happens when I create a new message. Let's say uh tell me a dad joke. Well, it's going to stream it and then it's
also going to stream the title. And that's the event stream that we were talking about as well. And of course, all this has persisted. So, if I was to go into um another chat, all of that stays in my users's information. If I was to say, "Hey, tell me a long story." We're not going to be able to access another page because we're waiting for this event stream or the stream rather to be completed. So, if I was to say, "Tell me a long story trying to change it to something else," it's not going to
happen until the full story has been shared. Working with AI and working with streaming within AI is incredibly useful. And now, especially with the Ustream hook and even use event stream hook that Larbell gives you, it just makes things easier. You don't have to worry about how you're going to have that two-way conversation and what you have to think through. It's easy on Laravel's side. It's easy on React and View side. So, why don't you make
something really cool with that? Check out Larhat, subscribe, and like this video so we can create more together.