Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add client data to nodes with multiple outputs #1542

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions nodes/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,37 @@ async function appendTopic (RED, config, wNode, msg) {
*/
function addConnectionCredentials (RED, msg, conn, config) {
if (config.includeClientData) {
if (!msg._client) {
msg._client = {}
}
RED.plugins.getByType('node-red-dashboard-2').forEach(plugin => {
if (plugin.hooks?.onAddConnectionCredentials && msg) {
msg = plugin.hooks.onAddConnectionCredentials(conn, msg)
// Add _client to each element
const addClientData = (item) => {
if (!item._client) {
item._client = {}
}
})
msg._client = {
...msg._client,
...{
socketId: conn.id,
socketIp: conn.handshake?.address
RED.plugins.getByType('node-red-dashboard-2').forEach(plugin => {
if (plugin.hooks?.onAddConnectionCredentials && item) {
item = plugin.hooks.onAddConnectionCredentials(conn, item)
}
})
item._client = {
...item._client,
...{
socketId: conn.id,
socketIp: conn.handshake?.address
}
}
return item
}

// Handle arrays and nested arrays
const processMsg = (data) => {
if (Array.isArray(data)) {
return data.map(item => processMsg(item))
} else if (typeof data === 'object' && data !== null) {
return addClientData(data)
}
return data
}

msg = processMsg(msg)
}
return msg
}
Expand Down
Loading