Skip to content

Commit

Permalink
refactor: rename event listener callback method (#21)
Browse files Browse the repository at this point in the history
* refactor: rename event listener callback method

* Update packages/client/src/interfaces.ts

Co-authored-by: Ariel Gentile <[email protected]>

---------

Co-authored-by: Ariel Gentile <[email protected]>
  • Loading branch information
gabrielmatau79 and genaris authored Oct 25, 2024
1 parent c406d2c commit 43e181a
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 51 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ jobs:
config-file: release-please-config.json
manifest-file: .release-please-manifest.json
token: ${{ secrets.GITHUB_TOKEN }}

- name: Print release outputs for debugging
continue-on-error: true
run: |
echo "Release outputs:"
echo ${{ toJson(steps.release-please.outputs) }}
# Initiate release process if release was created
# Initiate release process if release was created
release-client:
if: contains(needs.release-please.outputs.paths_released, 'packages/client')
runs-on: ubuntu-latest
Expand All @@ -86,15 +86,15 @@ jobs:
npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

release-server:
if: contains(needs.release-please.outputs.paths_released, 'packages/server')
runs-on: ubuntu-latest
needs: [release-please]
env:
DH_USERNAME: ${{secrets.DOCKER_HUB_LOGIN}}
DH_TOKEN: ${{secrets.DOCKER_HUB_PWD}}
IMAGE_NAME: 'message-pickup-repository'
IMAGE_NAME: 'message-pickup-repository'
IMAGE_TAG: ${{ github.ref == 'refs/heads/main' && 'dev' || github.ref }}
steps:
- name: Checkout message-pickup-repository
Expand All @@ -115,9 +115,9 @@ jobs:
- name: Build Docker image
run: |
cp yarn.lock packages/server
cd packages/server
docker build -f Dockerfile -t $DH_USERNAME/$IMAGE_NAME:$IMAGE_TAG .
cp yarn.lock packages/server
cd packages/server
docker build -f Dockerfile -t $DH_USERNAME/$IMAGE_NAME:$IMAGE_TAG .
- name: Push to DockerHub
run: docker push $DH_USERNAME/$IMAGE_NAME:$IMAGE_TAG
run: docker push $DH_USERNAME/$IMAGE_NAME:$IMAGE_TAG
10 changes: 5 additions & 5 deletions docs/message-pickup-repository-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The `MessagePickupRepositoryClient` is a client library designed to interact wit
1. [Installation](#installation)
2. [Available Methods](#available-methods)
- [connect()](#connect)
- [messageReceived()](#messageReceived)
- [messagesReceived()](#messagesReceived)
- [takeFromQueue()](#takeFromQueue)
- [getAvailableMessageCount()](#getAvailableMessageCount)
- [addMessage()](#addMessage)
Expand Down Expand Up @@ -40,9 +40,9 @@ Establishes a WebSocket connection to the server.

---

### `messageReceived(callback)`
### `messagesReceived(callback)`

Registers a callback to handle `messageReceive` events from the WebSocket server. This method will be primarily used to receive new messages that are published by the server.
Registers a callback to handle `messagesReceived` events from the WebSocket server. This method will be primarily used to receive new messages that are published by the server.

- **Parameters**:

Expand Down Expand Up @@ -178,7 +178,7 @@ Disconnects from the WebSocket server.
Here is a simple usage example:

```typescript
import { MessagePickupRepositoryClient } from './MessagePickupRepositoryClient'
import { MessagePickupRepositoryClient } from '@2060.io/message-pickup-repository-client'

async function runClient() {
const client = new MessagePickupRepositoryClient('ws://localhost:3500')
Expand All @@ -189,7 +189,7 @@ async function runClient() {
console.log('Connected to the WebSocket server.')

// Register message receive callback
client.messageReceived((data) => {
client.messagesReceived((data) => {
console.log('Received message:', data)
})

Expand Down
2 changes: 1 addition & 1 deletion docs/message-pickup-repository-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ To resolve synchronization across multiple instances the Message Pickup Reposito

- Then a new message is received, the server sends a JSON-RPC response to the client with the new message data.

- Once the server send JSON-RPC response, the client receives the notification with the message through its subscription to the `messageReceive` event. The client’s callback function is triggered with the message data.
- Once the server send JSON-RPC response, the client receives the notification with the message through its subscription to the `messagesReceived` event. The client’s callback function is triggered with the message data.

---

Expand Down
9 changes: 3 additions & 6 deletions packages/client/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

## [0.0.4](https://github.com/2060-io/message-pickup-repository/compare/@2060.io/[email protected][email protected]/[email protected]) (2024-10-18)


### Bug Fixes

* client git path ([#14](https://github.com/2060-io/message-pickup-repository/issues/14)) ([687a0be](https://github.com/2060-io/message-pickup-repository/commit/687a0be7bfa71e06619b63b8706c2f7b60d6ed4c))
- client git path ([#14](https://github.com/2060-io/message-pickup-repository/issues/14)) ([687a0be](https://github.com/2060-io/message-pickup-repository/commit/687a0be7bfa71e06619b63b8706c2f7b60d6ed4c))

## [0.0.3](https://github.com/2060-io/message-pickup-repository/compare/@2060.io/[email protected][email protected]/[email protected]) (2024-10-18)


### Bug Fixes

* client types and service methods into server ([#5](https://github.com/2060-io/message-pickup-repository/issues/5)) ([a3ba864](https://github.com/2060-io/message-pickup-repository/commit/a3ba864c9e1e22d29890722459443ad7d78330b9))
- client types and service methods into server ([#5](https://github.com/2060-io/message-pickup-repository/issues/5)) ([a3ba864](https://github.com/2060-io/message-pickup-repository/commit/a3ba864c9e1e22d29890722459443ad7d78330b9))

## [0.0.2](https://github.com/2060-io/message-pickup-repository/compare/@2060.io/[email protected]/[email protected]) (2024-10-18)


### Features

* :sparkles: initial setup to Message Pickup Repository gateway ([#2](https://github.com/2060-io/message-pickup-repository/issues/2)) ([bbf5f20](https://github.com/2060-io/message-pickup-repository/commit/bbf5f207ca63f95f1afe83e17523c23b39c6c841))
- :sparkles: initial setup to Message Pickup Repository gateway ([#2](https://github.com/2060-io/message-pickup-repository/issues/2)) ([bbf5f20](https://github.com/2060-io/message-pickup-repository/commit/bbf5f207ca63f95f1afe83e17523c23b39c6c841))
26 changes: 13 additions & 13 deletions packages/client/src/MessagePickupRepositoryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
RemoveAllMessagesOptions,
ConnectionIdOptions,
AddLiveSessionOptions,
MessageReceivedCallbackParams,
MessagesReceivedCallbackParams,
} from './interfaces'
import {
AddMessageOptions,
Expand All @@ -20,7 +20,7 @@ log.setLevel('info')
export class MessagePickupRepositoryClient implements MessagePickupRepository {
private client?: Client
private readonly logger = log
private messageReceivedCallback: ((data: MessageReceivedCallbackParams) => void) | null = null
private messagesReceivedCallback: ((data: MessagesReceivedCallbackParams) => void) | null = null

constructor(private readonly url: string) {}

Expand All @@ -37,11 +37,11 @@ export class MessagePickupRepositoryClient implements MessagePickupRepository {
client.on('open', () => {
this.logger.log(`Connected to WebSocket server ${client}`)

client.subscribe('messageReceive')
client.subscribe('messagesReceived')

client.addListener('messageReceive', (data) => {
if (this.messageReceivedCallback) {
this.messageReceivedCallback(data as MessageReceivedCallbackParams)
client.addListener('messagesReceived', (data) => {
if (this.messagesReceivedCallback) {
this.messagesReceivedCallback(data as MessagesReceivedCallbackParams)
} else {
this.logger.log('Received message event, but no callback is registered:', data)
}
Expand All @@ -65,28 +65,28 @@ export class MessagePickupRepositoryClient implements MessagePickupRepository {
}

/**
* Register a callback function for the 'messageReceive' event.
* This function allows you to set up a listener for the 'messageReceive' event,
* Register a callback function for the 'messagesReceived' event.
* This function allows you to set up a listener for the 'messagesReceived' event,
* which is triggered when a message is received via JSON-RPC.
*
* @param callback - The callback function to be invoked when 'messageReceive' is triggered.
* @param callback - The callback function to be invoked when 'messagesReceived' is triggered.
* The callback receives a `data` parameter of type `JsonRpcParamsMessage`, containing:
*
* @param {MessageReceivedCallbackParams} data - The data received via the 'messageReceive' event.
* @param {MessagesReceivedCallbackParams} data - The data received via the 'messagesReceived' event.
*
* @param {string} data.connectionId - The ID of the connection associated with the message.
* @param {QueuedMessage[]} data.message - Array of queued messages received.
* @param {string} [data.id] - (Optional) The identifier for the JSON-RPC message.
*
* @example
* messageReceived((data: MessageReceivedCallbackParams) => {
* messagesReceived((data: MessageReceivedCallbackParams) => {
* const { connectionId, message } = data
* console.log('ConnectionId:', data.connectionId);
* console.log('Message:', message[0].id)
* });
*/
messageReceived(callback: (data: MessageReceivedCallbackParams) => void): void {
this.messageReceivedCallback = callback
messagesReceived(callback: (data: MessagesReceivedCallbackParams) => void): void {
this.messagesReceivedCallback = callback
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface AddLiveSessionOptions {
sessionId: string
}

export interface MessageReceivedCallbackParams {
export interface MessagesReceivedCallbackParams {
connectionId: string
message: QueuedMessage[]
messages: QueuedMessage[]
}
9 changes: 3 additions & 6 deletions packages/server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@

## [0.0.4](https://github.com/2060-io/message-pickup-repository/compare/@2060.io/[email protected][email protected]/[email protected]) (2024-10-23)


### Bug Fixes

* multiple subscriptions to messages received during addLiveSession ([#17](https://github.com/2060-io/message-pickup-repository/issues/17)) ([37270a0](https://github.com/2060-io/message-pickup-repository/commit/37270a0b21ead05e5c2e2285d001bc8d030c26d4))
- multiple subscriptions to messages received during addLiveSession ([#17](https://github.com/2060-io/message-pickup-repository/issues/17)) ([37270a0](https://github.com/2060-io/message-pickup-repository/commit/37270a0b21ead05e5c2e2285d001bc8d030c26d4))

## [0.0.3](https://github.com/2060-io/message-pickup-repository/compare/@2060.io/[email protected][email protected]/[email protected]) (2024-10-21)


### Bug Fixes

* client types and service methods into server ([#5](https://github.com/2060-io/message-pickup-repository/issues/5)) ([a3ba864](https://github.com/2060-io/message-pickup-repository/commit/a3ba864c9e1e22d29890722459443ad7d78330b9))
- client types and service methods into server ([#5](https://github.com/2060-io/message-pickup-repository/issues/5)) ([a3ba864](https://github.com/2060-io/message-pickup-repository/commit/a3ba864c9e1e22d29890722459443ad7d78330b9))

## [0.0.2](https://github.com/2060-io/message-pickup-repository/compare/@2060.io/[email protected]/[email protected]) (2024-10-18)


### Features

* :sparkles: initial setup to Message Pickup Repository gateway ([#2](https://github.com/2060-io/message-pickup-repository/issues/2)) ([bbf5f20](https://github.com/2060-io/message-pickup-repository/commit/bbf5f207ca63f95f1afe83e17523c23b39c6c841))
- :sparkles: initial setup to Message Pickup Repository gateway ([#2](https://github.com/2060-io/message-pickup-repository/issues/2)) ([bbf5f20](https://github.com/2060-io/message-pickup-repository/commit/bbf5f207ca63f95f1afe83e17523c23b39c6c841))
4 changes: 2 additions & 2 deletions packages/server/src/websocket/interfaces/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { QueuedMessage } from '@credo-ts/core'

export interface JsonRpcParams {
connectionId: string
message: QueuedMessage[]
messages: QueuedMessage[]
id: string
}

export interface JsonRpcResponseSubscriber {
jsonrpc: '2.0'
method: 'messageReceive'
method: 'messagesReceived'
params: JsonRpcParams
}
2 changes: 1 addition & 1 deletion packages/server/src/websocket/websocket.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class WebsocketGateway implements OnModuleInit, OnModuleDestroy {
port: WS_PORT,
})

this.server.event('messageReceive')
this.server.event('messagesReceived')
this.server.createError(500, 'Error initializing WebSocket server', {
jsonrpc: '2.0',
result: 'error',
Expand Down
6 changes: 3 additions & 3 deletions packages/server/src/websocket/websocket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ export class WebsocketService {
* @param {string} socket_id - The unique identifier of the WebSocket client to which the message will be sent.
* @param {JsonRpcResponseSubscriber} message - The message to send, including:
* @property {string} jsonrpc - The JSON-RPC version, always '2.0'.
* @property {string} method - The method being invoked, in this case, 'messageReceive'.
* @property {string} method - The method being invoked, in this case, 'messagesReceived'.
* @property {Object} params - An object containing:
* @property {string} connectionId - The ID of the connection associated with the message.
* @property {QueuedMessage[]} message - An array of messages to be sent.
Expand Down Expand Up @@ -680,10 +680,10 @@ export class WebsocketService {
// Parse and process the received message, and construct the JSON-RPC response
const jsonRpcResponse: JsonRpcResponseSubscriber = {
jsonrpc: '2.0',
method: 'messageReceive',
method: 'messagesReceived',
params: {
connectionId: channel, // The channel is treated as the connectionId
message: JSON.parse(message), // Parse the message to ensure it's valid JSON
messages: JSON.parse(message), // Parse the message to ensure it's valid JSON
id: '',
},
}
Expand Down
4 changes: 2 additions & 2 deletions packages/server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"compilerOptions": {
"baseUrl": ".",
"types": ["node", "jest"],
"types": ["node", "jest"]
},
"exclude": ["node_modules", "dist"]
}
}

0 comments on commit 43e181a

Please sign in to comment.