Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
730 Lượt xem

Good day everyone, I'm currently building a custom module and utilizing its controllers to create an external api with custom logic for endpoints like /api/auth/login , /api/auth/whoami etc. Basically it's an attendance logging api for my custom module and is integrated to a mobile application. Currently, I was looking how to make it so that my endpoint for attendnace logging could send notifications to the mobile application. I was thinking of utilizing a websocket and odoo has it's own websocket but i'm having a hard time how to implement it. For reference, here is the endpoint where i want to integrate a notification and basically establish a websocket connection. 


 # ───────────────────────────────────────────────────────────────

    # ATTENDANCE  –  /api/attendance/log  (POST)

    # ───────────────────────────────────────────────────────────────

    @http.route(

        "/api/attendance/log", type="http", auth="public", csrf=False, methods=["POST"]

    )

    def log_attendance(self):

        user = self._current_user()

        if not user:

            return self._json({"error": "Unauthorized"}, 401)

        if user.share:

            return self._json({"error": "Access denied: internal users only"}, 403)


        try:

            data = json.loads(request.httprequest.data or "{}")

        except Exception:

            return self._json({"error": "Malformed JSON body"}, 400)


        enrollment_id = data.get("enrollment_id")

        log_type = data.get("log_type")

        if not enrollment_id or not log_type:

            return self._json({"error": "enrollment_id and log_type are required"}, 400)


        enrollment = request.env["sis.enrollment"].sudo().browse(int(enrollment_id))

        if not enrollment.exists():

            return self._json({"error": "Student does not exist"}, 404)


        try:

            log = (

                request.env["sis.student.attendance.log"]

                .sudo()

                .create(

                    {

                        "enrollment_id": enrollment.id,

                        "log_type": log_type,

                    }

                )

            )

        except ValidationError as ve:

            return self._json({"error": str(ve)}, 400)

        except Exception as e:

            _logger.exception("Failed to log attendance")

            return self._json({"error": str(e)}, 500)


        return self._json(

            {

                "status": "success",

                "log_id": log.id,

                "student_name": enrollment.full_name,

                "section_name": enrollment.section_id.display_name,

            },

            201,

        )

So basically it is an attendance logging of a student and i want to notify their parent in the mobile application once their son/daughter times in/out of their school

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi John,

Odoo’s “websocket” is really the longpolling bus that powers live chat, notifications, and Discuss. It’s not a raw websocket server you can hook into directly, but you can piggyback on it.

For your case (attendance → push update to mobile app), the usual pattern is:

  1. Use the bus.bus model

    request.env['bus.bus'].sudo().sendone( f"attendance_{enrollment_id}", { "student": enrollment.full_name, "section": enrollment.section_id.display_name, "log_type": log_type, } )

    This publishes a message on a custom channel.
  2. Subscribe from your client
    Your mobile app (or another backend service) connects to /longpolling/poll with the channel name (attendance_123). Whenever you sendone, the subscriber gets the payload.
  3. Alternative: external websocket
    If you really need true websockets, you’ll have to spin up a small websocket server (Node, FastAPI, etc.), and then trigger it from Odoo (via HTTP call, RabbitMQ, Redis, etc.) whenever a log is created.

So if you want to stay “inside” Odoo, stick to the bus/longpolling. If you want a native websocket to push directly to your mobile app, you’ll need a separate service alongside Odoo.

Hope that clears it up!

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 9 25
273
2
thg 8 25
466
2
thg 8 25
564
2
thg 8 25
609
0
thg 8 25
560