MagicJS
mern.aiUniversityGitHub
  • Introduction to MagicJS
  • Why MagicJS?
  • Getting Started & Installation
  • Basic Guide
    • Create a new page using React
    • Navigate between pages
    • Create an API and integrate it with the frontend
    • Authenticate Users
      • Authorise based on Roles
    • Advanced State Management with useContent()
    • Perform CRUD Operations
    • Adding Realtime capabilities using socket
    • Handling file uploads and downloads
  • Advanced Guide
    • Understanding the concept of features in MagicJS
    • Using UI components & functions across multiple Magic Features
    • Advanced Routing of pages
    • Enable SSR
    • Access MongoDB
    • Styling pages using Tailwind CSS
  • Deploying
  • Update MagicJS
  • API References
    • Frontend
      • <LinkDisplay>
      • createSrc()
      • createUploader()
      • importUI()
      • loadConfig()
      • protected()
      • useParams()
      • useAxios()
      • useLogin()
      • useSocket()
      • useContent()
      • usePromise()
      • useNotification()
    • Backend
      • createBackendFunction()
      • data()
      • io()
      • ServerInstance()
      • utils
        • hash()
        • verifyHash()
        • initiateEmailVerification()
        • saveFileToUserUploads()
        • readFileFromUserUploads()
        • removeFileFromUserUploads()
        • assignRoleToUser()
        • unassignRoleFromUser()
        • findAllRolesByUser()
        • isUserInAnyRoles()
        • assignRoleToUser()
Powered by GitBook
On this page

Was this helpful?

  1. API References
  2. Backend

io()

Previousdata()NextServerInstance()

Last updated 1 year ago

Was this helpful?

This function enables real-time, bidirectional communication between web clients and servers commonly used for features like real-time chat, live updates and collaborative editing in web applications.

Example:

import { createBackendFunction, io } from "@magicjs.dev/backend";
import moment from "moment";

export default createBackendFunction(async function () {
    try {
        io().to(`public/room`).emit(`refresh`);
    } catch (error) {}
});

When the backend function is invoked, potentially in response to an API request, it emits a "refresh" event to all clients within the "public/room" using the io module. This event can be intercepted on the client side, enabling real-time communication and supporting actions such as triggering a refresh or update on the client interface. This mechanism serves as a means to broadcast information to all connected clients in the specified room, fostering synchronized updates across multiple users in a real-time manner.

Click here to refer GitHub.