Skillpedia
واجهات API والخوادم (backend)8.1k · #12592

webiny-websocket-notifications

ما الذي تفعله

ترشد هذه المهارة ذكاءك عندما يحتاج الخادم إلى إخبار مستخدم الإدارة بعمل قيد التنفيذ. وهي لمطورين يريدون تحديثات حية في لوحة Webiny.

متى تستخدمها

  • ✓عندما تحتاج مهمة في الخلفية إلى إظهار التقدم للمستخدم الذي بدأها.
  • ✓عندما يجب أن تتحدث شاشة الإدارة تلقائيًا بعد تغيير في الخادم.
  • ✓عندما يجب أن تظهر الرسالة كإشعار في تطبيق الإدارة.

رابط المهارة

https://github.com/webiny/webiny-js/blob/next/skills/user-skills/api/websocket-notifications/SKILL.md

انسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.

طريقة الاستخدام

أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.

رسالة جاهزة للصق

استخدم هذه المهارة لمساعدتي: https://github.com/webiny/webiny-js/blob/next/skills/user-skills/api/websocket-notifications/SKILL.md اقرأ ملف SKILL.md في هذا الرابط ونفّذ تعليماته لمهمتي.
تريد أن يجد الذكاء الاصطناعي مهارات مثل هذه بنفسه؟ثبّت خادم Skillpedia MCP المجاني: سطر واحد، ويبحث في 15,000+ مهارة قبل كل مهمة.ثبّته في دقيقة واحدة →

مثال على ما تطلبه من الذكاء الاصطناعي

“أظهر إشعارًا في الإدارة عندما تنتهي مهمة الخصم الجماعي لكل منتج.”

5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.

كيف تعمل

في الخادم، يرسل الكود رسالة إلى المستخدم الذي بدأ العمل، دون أن يتسبب فشل الرسالة في إفشال العمل الفعلي. وفي تطبيق الإدارة، يفلتر معالج الرسائل حسب الإجراء ويتفاعل معها، كأن يعرض إشعارًا.

المحتوى التقني (ملف SKILL.md الأصلي)

النص الأصلي من المؤلف، مباشرة من GitHub.

Websocket notifications (API → Admin)

TL;DR

On the API, inject WebsocketsSendToIdentityUseCase (webiny/api) + IdentityContext (webiny/api/security) and call sendToIdentity.execute({ id }, { action, data }). On the Admin, implement a WebsocketEventHandler (webiny/admin/websockets) that filters on action and reacts (e.g. toast via Notifications from webiny/admin), registered with createFeature + RegisterFeature.

API — emit

import { WebsocketsSendToIdentityUseCase } from "webiny/api";
import { IdentityContext } from "webiny/api/security";

class MyTaskOrHook {
  constructor(
    private identityContext: IdentityContext.Interface,
    private sendToIdentity: WebsocketsSendToIdentityUseCase.Interface
  ) {}

  async notify(entry) {
    // Best-effort: a websocket failure must never fail the real work.
    try {
      const identity = this.identityContext.getIdentity();
      if (identity) {
        await this.sendToIdentity.execute(
          { id: identity.id },
          {
            action: "cms.product.discountApplied",
            data: { id: entry.entryId, price: entry.values.price }
          }
        );
      }
    } catch (ex) {
      // log & swallow
    }
  }
}
// dependencies: [IdentityContext, WebsocketsSendToIdentityUseCase]
  • Send to the user who triggered the work — get them from IdentityContext. In a background task/bulk action, the triggering identity is available.
  • Use a namespaced action string; put the payload in data.
  • Sender data type: { action?: string; data?: T; error?: {...} }.

Admin — listen

import { WebsocketEventHandler } from "webiny/admin/websockets";
import { Notifications } from "webiny/admin";

const ACTION = "cms.product.discountApplied";

class MyHandlerImpl implements WebsocketEventHandler.Interface {
  constructor(private notifications: Notifications.Interface) {}

  async handle(event: WebsocketEventHandler.Event): Promise<void> {
    const payload = event.payload as { action?: string; data?: { id: string; price: number } };
    if (payload.action !== ACTION || !payload.data) {
      return; // every handler sees every message — filter by action
    }
    this.notifications.success({
      title: "Discount applied",
      description: `New price ${payload.data.price}.`
    });
  }
}

export const MyHandler = WebsocketEventHandler.createImplementation({
  implementation: MyHandlerImpl,
  dependencies: [Notifications]
});

Read the message off event.payload — event.payload.action and event.payload.data (the exact { action, data } object the API sent).

Admin — register

Register the handler in a feature and render it from your Admin.Extension:

import { createFeature, RegisterFeature } from "webiny/admin";
import { MyHandler } from "./MyHandler.js";

const MyFeature = createFeature({
  name: "MyExtension/Notifications",
  register(container) {
    container.register(MyHandler);
  }
});

export default () => <RegisterFeature feature={MyFeature} />;

The websockets runner resolves every registered WebsocketEventHandler and calls handle for each incoming message — hence the action filter in each handler.

Related

  • webiny-cms-bulk-actions — the typical emitter: a bulk action's processData sends a message per processed entry so the Admin can toast progress live.
الأسئلة الشائعة

أي إصدار من Webiny يحتاجه؟

تقول المهارة إن إشعارات websocket تحتاج إلى Webiny 6.5.0 أو أحدث.

ماذا إذا فشل إرسال الرسالة؟

تقول المهارة إن فشل websocket يجب ألا يُفشل العمل الفعلي، لذلك تُسجَّل الأخطاء وتُتجاهل.

من يستقبل الرسالة؟

المستخدم الذي بدأ العمل، ويُعرف عبر سياق الهوية.

التفاصيل

الوصف الأصلي من المؤلف

Sending real-time notifications from the API to the Admin app over websockets, and reacting to them on the client. Use this skill when the developer wants to push a message to the user who triggered some server-side work (e.g. per-entry progress from a background task/bulk action) and show a toast, update a cache, or refresh UI in response. Requires Webiny 6.5.0 or newer.

repo: webiny/webiny-js

الشعبية

نجوم GitHub
8.1k
الترتيب
#12592
الترخيص
—
آخر تحديث
2‏/11‏/2026

هل هذه المهارة مفيدة؟

0

سجّل الدخول للتصويت.

التعليقات

  • …

المزيد من المهارات من هذا المستودع

مهارات مشابهة