Issue with Real-Time Tracking in Frappe: Guest User Data Creation Instead of Logged-In User

262 Views Asked by At

I am currently working on implementing real-time tracking using sockets in Frappe. My goal is to add latitude and longitude data to a custom Doctype for a logged-in user. To ensure secure authentication, I am utilizing the API key of the logged-in user. However, I am facing an issue where the data is being created by the guest user instead of the logged-in user.

websocket.js code:

const WebSocket = require('ws');
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());

const server = require('http').createServer(app);
const wss = new WebSocket.Server({ server });

wss.on('connection', (ws) => {
    console.log('New WebSocket connection established.');

    ws.send('Welcome to the server!');

    ws.on('message', async (message) => {
        console.log('Message from client:', message);

        try {
            const data = JSON.parse(message);
            const { latitude, longitude } = data;

            // Step 5: Get the logged-in user ID from the WebSocket client (you need to implement this part on the client-side)
            const user = "";

            // Step 6: Call the server-side function to insert the data into the Location Data table
            const response = await insertLocationData(latitude, longitude, user);

            console.log('Server response:', response);
        } catch (error) {
            console.error('Error processing message:', error);
        }
    });
});

async function insertLocationData(latitude, longitude, user) {
    try {
        // Step 7: Make an HTTP POST request to the Frappe server to insert the data
        // Note: Replace 'YOUR_FRAPPE_API_KEY' with your actual API key
        const apiKey = 'f6cf9af8241808c6';
        const url = 'http://redlines.local:8000/api/resource/Location Data';
        const postData = JSON.stringify({ latitude, longitude, user });

        const http = require('http');
        const options = {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${apiKey}`,
                'Content-Type': 'application/json',
                'Content-Length': postData.length
            }
        };

        return new Promise((resolve, reject) => {
            const req = http.request(url, options, (res) => {
                let data = '';

                res.on('data', (chunk) => {
                    data += chunk;
                });

                res.on('end', () => {
                    const responseData = JSON.parse(data);
                    resolve(responseData);
                });
            });

            req.on('error', (error) => {
                reject(error);
            });

            req.write(postData);
            req.end();
        });
    } catch (error) {
        console.error('Error inserting location data:', error);
        throw error;
    }
}

const port = 9002;
server.listen(port, () => {
    console.log(`Server is listening on port ${port}`);
});

Has anyone encountered a similar problem or has experience with real-time tracking in Frappe using sockets? I would greatly appreciate any guidance or suggestions on how to successfully implement real-time tracking in frappe. Thank you in advance!

0

There are 0 best solutions below