how to get data real time with feathersJS, SocketIO

21 Views Asked by At

This my feather app i want to when calling http://localhost:3030/notification_message, send real-time data.

// 1.notification_message.schema.js
import { resolve } from '@feathersjs/schema'
import { Type, getValidator, querySyntax } from '@feathersjs/typebox'
import { dataValidator, queryValidator } from '../../validators.js'

// Main data model schema
export const notificationMessageSchema = Type.Object(
  {
    id: Type.Number(),
    sender_user_id: Type.Number(),
    to_user_id: Type.Number(),
    message: Type.String(),
    details: Type.String(),
    link: Type.String(),
    is_check: Type.Boolean(),
    created_at: Type.String(),

  },
  { $id: 'NotificationMessage', additionalProperties: false }
)
export const notificationMessageValidator = getValidator(notificationMessageSchema, dataValidator)
export const notificationMessageResolver = resolve({})

export const notificationMessageExternalResolver = resolve({})

// Schema for creating new entries
export const notificationMessageDataSchema = Type.Pick(notificationMessageSchema, ['sender_user_id','to_user_id','message','details','link','is_check' ], {
  $id: 'NotificationMessageData'
})
export const notificationMessageDataValidator = getValidator(notificationMessageDataSchema, dataValidator)
export const notificationMessageDataResolver = resolve({
  created_at: async () => {
    return new Date().toISOString();
  }
  
})

// Schema for updating existing entries
export const notificationMessagePatchSchema = Type.Partial(notificationMessageSchema, {
  $id: 'NotificationMessagePatch'
})
export const notificationMessagePatchValidator = getValidator(notificationMessagePatchSchema, dataValidator)
export const notificationMessagePatchResolver = resolve({})

// Schema for allowed query properties
export const notificationMessageQueryProperties = Type.Pick(notificationMessageSchema, ['id','sender_user_id','to_user_id','message','details','link','is_check','created_at'])
export const notificationMessageQuerySchema = Type.Intersect(
  [
    querySyntax(notificationMessageQueryProperties),
    // Add additional query properties here
    Type.Object({}, { additionalProperties: false })
  ],
  { additionalProperties: false }
)
export const notificationMessageQueryValidator = getValidator(notificationMessageQuerySchema, queryValidator)
export const notificationMessageQueryResolver = resolve({})
this is notification_message.schema.js in my feathersJS this file sets up a comprehensive schema, validators, and resolvers for handling notification messages in a FeathersJS application, covering data creation, updating, querying, and external use.
// 2.notification_message.js
import { authenticate } from '@feathersjs/authentication'

import { hooks as schemaHooks } from '@feathersjs/schema'
import {
  notificationMessageDataValidator,
  notificationMessagePatchValidator,
  notificationMessageQueryValidator,
  notificationMessageResolver,
  notificationMessageExternalResolver,
  notificationMessageDataResolver,
  notificationMessagePatchResolver,
  notificationMessageQueryResolver
} from './notification_message.schema.js'
import { NotificationMessageService, getOptions } from './notification_message.class.js'

export const notificationMessagePath = 'notification_message'
export const notificationMessageMethods = ['find', 'get', 'create', 'patch', 'remove']

export * from './notification_message.class.js'
export * from './notification_message.schema.js'

// A configure function that registers the service and its hooks via `app.configure`
export const notificationMessage = (app) => {
  // Register our service on the Feathers application
  app.use(notificationMessagePath, new NotificationMessageService(getOptions(app)), {
    // A list of all methods this service exposes externally
    methods: notificationMessageMethods,
    // You can add additional custom events to be sent to clients here
    events: []
  })
  // Initialize hooks
  app.service(notificationMessagePath).hooks({
    around: {
      all: [
        //authenticate('jwt'),
        schemaHooks.resolveExternal(notificationMessageExternalResolver),
        schemaHooks.resolveResult(notificationMessageResolver)
      ]
    },
    before: {
      all: [
        schemaHooks.validateQuery(notificationMessageQueryValidator),
        schemaHooks.resolveQuery(notificationMessageQueryResolver)
      ],
      find: [],
      get: [],
      create: [
        schemaHooks.validateData(notificationMessageDataValidator),
        schemaHooks.resolveData(notificationMessageDataResolver)
      ],
      patch: [
        schemaHooks.validateData(notificationMessagePatchValidator),
        schemaHooks.resolveData(notificationMessagePatchResolver)
      ],
      remove: []
    },
    after: {
      all: []
    },
    error: {
      all: []
    }
  })
}
3.app.js this file initializes a Feathers.js application, configures middleware, sets up services, channels, and hooks, and exports the configured app. The application seems to be using Koa for HTTP handling, Socket.io for real-time communication, and PostgreSQL as a database.
// 3.app.js
import { feathers } from '@feathersjs/feathers'
import configuration from '@feathersjs/configuration'
import { koa, rest, bodyParser, errorHandler, parseAuthentication, cors, serveStatic } from '@feathersjs/koa'
import socketio from '@feathersjs/socketio'

import { configurationValidator } from './configuration.js'
import { logError } from './hooks/log-error.js'
import { postgresql } from './postgresql.js'

import { services } from './services/index.js'
import { channels } from './channels.js'

const app = koa(feathers())

// Load our app configuration (see config/ folder)
app.configure(configuration(configurationValidator))

// Set up Koa middleware
app.use(cors())
app.use(serveStatic(app.get('public')))
app.use(errorHandler())
app.use(parseAuthentication())
app.use(bodyParser())

// Configure services and transports
app.configure(rest())
app.configure(
  socketio({
    cors: {
      origin: app.get('origins')
    }
  })
)
app.configure(postgresql)

app.configure(services)
app.configure(channels)

// Register hooks that run on all service methods
app.hooks({
  around: {
    all: [logError]
  },
  before: {},
  after: {},
  error: {}
})
// Register application setup and teardown hooks here
app.hooks({
  setup: [],
  teardown: []
})

export { app }

0

There are 0 best solutions below