How can I implement server sent events

How can I implement server sent events in the actions.ts? I understand that all the backend functions are added to actions.ts, but I would like to add a server sent event function to actions.ts Thanks for the help!
6 Replies
kapa.ai
kapa.ai5mo ago
Hi @haarismian. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Hmm, I don't know enough to give you a confident answer yet. The knowledge sources provided do not contain specific information on implementing server-sent events in Wasp's actions.ts. However, Wasp does support web sockets, which could potentially be used for server-sent events. You can read more about it in the Wasp documentation on Web Sockets (https://wasp-lang.dev/docs/advanced/web-sockets#usesocketlistener-hook). For more specific guidance on server-sent events, I recommend checking the official Wasp documentation or reaching out to the Wasp community for support. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @haarismian, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
martinsos
martinsos5mo ago
@haarismian what exactly do you mean by server sent events? Does that mean server initiating something against th eclient? As Kapa said, sockets are likely a soltion for you in that case. Or you can impelment some kind of constant polling on the client.
haarismian
haarismian5mo ago
Hey @martinsos , thanks for replying, I ended up using this to implement. My goal was to implement streaming of text, I was under the impression one method was with server sent events. I found an old github page where the opensaas team has an example of streaming the text response from openai by making a custom api in the main.wasp
martinsos
martinsos5mo ago
I imagine you maybe found https://github.com/wasp-lang/wasp/tree/main/examples/streaming ? Thta one goes with the API, yes! I hope that worked for you!
GitHub
wasp/examples/streaming at main · wasp-lang/wasp
The fastest way to develop full-stack web apps with React & Node.js. - wasp-lang/wasp
haarismian
haarismian5mo ago
Hey @martinsos @kapa.ai it did work. Can I ask you for a bit of help again, I can't seem to figure out why in this component the function usetextstream is called every time state changes, this is making it so my API call just runs no matter what changes, any idea how I can change it to only be called when the button is pressed? I have been stuck on this for a few days now lol, I don't know how to convert the example you shared to only run when the button is pressed
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- I'm sorry, but I can't provide the help you're looking for. The knowledge sources provided do not contain any information about a usetextstream function or how to control when it's called. It's also important to note that I can only provide information based on the provided knowledge sources. However, in general, if you want a function to be called only when a button is pressed in a React component, you would typically attach that function to the button's onClick event. Here's a basic example:
<button onClick={yourFunction}>Click me</button>
<button onClick={yourFunction}>Click me</button>
In this example, yourFunction would only be called when the button is clicked. For more specific help, I recommend referring to the React documentation or asking for help in a relevant coding community. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @haarismian, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Want results from more Discord servers?
Add your server