Send and receive socket without opening the html web page

I am having a django project where I have chat.html, journey.html, and manager.html.

One of the flows is that I am sending socket for triggering journey from chat.html. And in the journey.html, I have a flow for approval where the socket gets sent to manager.html.

This flow is working only when I keep all these 3 pages open in browser. But if I don’t keep the “journey” page open in a browser, the socket is not getting received and send is not happening from “journey”.

This should work without the need to open “journey” page in the browser. How can I do that?

Welcome @gayathri-kannanv !

I’m not sure I’m following what you’re trying to describe here by using the phrase “sending socket”. You can send data through a socket from one page to another, but you don’t send the socket itself.

Please describe in more detail the technical architecture of your application

  • What type of sockets are you talking about here? Are you using websockets or some other type of network socket? (Or some other type of socket?)

  • What are you connecting these sockets to?

Note: If you’re talking about websockets and Django, you should be using Channels. If you’re trying to manage a websocket or any other type of network socket using a regular Django application, that’s wrong. Don’t try to do that, it’s not going to work right.

chat.html

const socket = new WebSocket('ws://' + window.location.host + '/ws/chat/room1/');
...
async function trigger_journey() {
    var selectedJourney = document.getElementById("journey_dropdown").value;
    const response = await fetch(`/fetch_journey/?journey_name=${selectedJourney}`);
    const data = await response.json();
    console.log("data:", data)
    const elementId = 'messageDisplay';
    const chatContainer = document.getElementById(elementId);
    socket.send(JSON.stringify({ type: 'trigger_journey', sender: 'Agent', journeyid: data.journeyid, journeyorder: data.journeyorder, requestId: requestId, email: email, name: uname, phone: phone, chat: chatContainer.innerHTML, department: Department }));
}

journey.html

socket.onmessage = function (event) {
    const message = JSON.parse(event.data);
           
    if (message.type === 'trigger_journey') {
        requestid = message.requestid;
        journeyid = message.journeyid;
        journeyorder = message.journeyorder;
        chat = message.chat;
        department = message.department;
        ...
                     
                if (activityid === 'emailevent') {    
                    sendEmail();
                }              
                else if (activityid === 'approvalevent') {
                    sendApproval()   
                }
            });
    }
}
            
async function sendApproval() {
    ...
    if (sendToForExistingJourneyId === "Manager") {
        data = {
            'type': 'manager_approval',
            'sendTo': sendTo,
            'requestId': requestid,
            'name': uname,
            'phoneNumber': phoneNumber,
            'requestDetails': ureqdetails,
            'sender': "agent"
        };
        //"sent to manager");
    }
    socket.send(JSON.stringify(data));

manager_dashboard.html

socket.onmessage = function(event) {
    //"--------------manager_approval--------------")
    var data = JSON.parse(event.data);
    //data)
    if (data.type === 'manager_approval' ) {
        handleWebSocketMessage(data);
    }
   
};

This is the functionality I am trying. Hope it’s understandable.

So there are two parts where data is send via django sockets. One is from chat.html to journey and depending upon the activity if that is ‘manager_approval’ in this case, it triggers that function to send data to manager via socket.

But what happens is this flow doens’t work when I don’t open the intermediate page journey.html in browser. I’ll need to open all three pages for this to work.

I’m finding it difficult to understand here. These snippets don’t seem to be complete? It looks like you’re only opening the websocket in one of the pages?

I also need to see what you’re doing on the Django side for this.

What does your consumer look like?