Giter Club home page Giter Club logo

Comments (5)

pgjones avatar pgjones commented on July 18, 2024 2

Does this help https://medium.com/@pgjones/websockets-in-quart-f2067788d1ee ?

from quart.

pgjones avatar pgjones commented on July 18, 2024

As you describe what you'd like to do, I think you could stream the response, for example have you looked at Server Sent Events? I think a websocket would be better if you wanted continuous two way communication between the client and server.
Have you also looked at the aiohttp client example, as it is the best way to fetch the data.

This is a rough guess as to what you want, I've not tried it but the basic idea is hopefully clear,

import aiohttp
from quart_tutorial import ServerSentEvent  # Pseudo code
from quart import request

@app.route('/')
async def index():
    param = request.args.get('param', 'default_value')

    async def send_results_as_fetched():
        async with aiohttp.ClientSession() as session:
            for url in urls_to_fetch(param):
                async with session.get(url) as response:
                    data = await response.json()
                    yield ServerSentEvent(data)

    return send_results_as_fetched(), {
        'Content-Type': 'text/event-stream',
        'Cache-Control': 'no-cache',
        'Transfer-Encoding': 'chunked',
    }
var es = new EventSource('/');
es.onmessage = function (event) {
  ....

from quart.

davegravy avatar davegravy commented on July 18, 2024

Very much so, thanks for that!

from quart.

trashcluster avatar trashcluster commented on July 18, 2024

Hi, i don't know if i should open a new issue or just continue this thread :D
I'm completely new to WebSockets and Flask and Quart and Async python programming hence my extreme headache after 3 days of non stop trying to figure on how to send websocket data from a trigger.
I have an endpoint with a websocket client connecting to a Quart server and it works nicely with websockets functions. What i want is being able to send websocket messages when i call an http url.
I the end i want to control that websocket client device by doing api calls on a quart app.
As deep as i read the documentation i can't make sens of how to do this. Any tips ?

from quart.

pgjones avatar pgjones commented on July 18, 2024

@trashcluster It isn't clear to me what you are asking, nor what you want to achieve. If by websocket message you mean JSON encoded data, then you can send that either via a WebSocket message or a request body.

Probably be better to continue the conversation here.

from quart.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.