Skip to content

Commit

Permalink
add pingpong producers consumers, emit pingpong from ws
Browse files Browse the repository at this point in the history
  • Loading branch information
IkeHunter committed Sep 5, 2024
1 parent ab9ac97 commit 208aca9
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 6 deletions.
10 changes: 10 additions & 0 deletions server/docs/swagger_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@
}
}
},
"/api/ping": {
"get": {
"description": "",
"responses": {
"default": {
"description": ""
}
}
}
},
"/api/spotify/login": {
"get": {
"tags": [
Expand Down
5 changes: 3 additions & 2 deletions server/events/consumers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createConsumer } from '@jukebox/lib'
import { createConsumer, logger } from '@jukebox/lib'

export const registerConsumers = async () => {
createConsumer('ping-pong', (data) => {
console.log('pong')
const logData = data ?? 'No data received.'
logger.info(`Pong: ${logData}`)
})
}
3 changes: 3 additions & 0 deletions server/events/producers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createProducer } from '@jukebox/lib'

export const producePing = createProducer('ping-pong')
3 changes: 2 additions & 1 deletion server/routes/router.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Router } from 'express'
import { docsMiddleware } from '../middleware/docsMiddleware'
import * as baseViews from '../views/baseViews'
import { groupRoutes } from './groupRoutes'
import { spotifyRouter } from './spotifyRoutes'
import { userRouter } from './userRoutes'
import { docsMiddleware } from '../middleware/docsMiddleware'

const router = Router()
router.get('/', baseViews.healthcheck)
router.get('/api', baseViews.apiHelp)
router.get('/api/ping', baseViews.pingPongView)

router.use(docsMiddleware)
router.use('/api/spotify', spotifyRouter)
Expand Down
6 changes: 6 additions & 0 deletions server/views/baseViews.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { producePing } from 'server/events/producers'
import { apiRequest } from 'server/utils'

/**
Expand Down Expand Up @@ -35,3 +36,8 @@ export const apiHelp = apiRequest((req, res) => {
documenation: `${baseUrl}/api/docs/`
}
})

export const pingPongView = apiRequest((req, res) => {
const data = 'Ping from api.'
producePing(new Date().toLocaleTimeString(), data)
})
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"server/*": ["./server/*"],
"websocket/*": ["./websocket/*"],
"common/*": ["./common/*"],
"*": ["./*", "./server/*", "./websocket/*", "./common/*"]
"*": ["./*", "./server/*", "./websocket/*",]
} /* Specify a set of entries that re-map imports to additional lookup locations. */,
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
"typeRoots": [
Expand Down
14 changes: 14 additions & 0 deletions websocket/events/consumers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Kafka Consumers
*/

import { createConsumer, logger } from '@jukebox/lib'
import { socketEmit } from 'websocket/lib'

export const registerKafkaConsumers = () => {
createConsumer('ping-pong', (data) => {
socketEmit('ping-pong', data)
const logData = data ?? 'No data received.'
logger.info(`Pong: ${logData}`)
})
}
2 changes: 1 addition & 1 deletion websocket/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
* @description Manage ingress and egress of data from Kafka
* or other event driven data controllers.
*/
export * from './kafkaConsumers'
export * from './consumers'
1 change: 0 additions & 1 deletion websocket/events/kafkaConsumers.ts

This file was deleted.

7 changes: 7 additions & 0 deletions websocket/events/producers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Kafka Producers
*/

import { createProducer } from '@jukebox/lib'

export const producePing = createProducer('ping-pong')
2 changes: 2 additions & 0 deletions websocket/events/socketHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { handleSocketSubscribe } from 'websocket/controllers'
import type { CustomSocket, SocketNext } from 'websocket/types'
import { producePing } from './producers'

export const socketHandlers = (socket: CustomSocket, next: SocketNext) => {
socket.on('subscribe', (data) => handleSocketSubscribe(socket, data))
socket.on('ping-pong', (data) => producePing(new Date().toLocaleTimeString(), data))

next()
}

0 comments on commit 208aca9

Please sign in to comment.