# Send and receive socket without opening the html web page

**URL:** https://forum.djangoproject.com/t/send-and-receive-socket-without-opening-the-html-web-page/30031
**Category:** Async/Channels
**Created:** [April 10, 2024, 11:23am UTC](https://forum.djangoproject.com/t/send-and-receive-socket-without-opening-the-html-web-page/30031 "2024-04-10T11:23:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![gayathri-kannanv](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/gayathri-kannanv/32/20510_2.png) [@gayathri-kannanv](https://forum.djangoproject.com/u/gayathri-kannanv)
#### Post date: [April 10, 2024, 11:23am UTC](https://forum.djangoproject.com/t/send-and-receive-socket-without-opening-the-html-web-page/30031/1 "2024-04-10T11:23:04Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [April 10, 2024, 12:00pm UTC](https://forum.djangoproject.com/t/send-and-receive-socket-without-opening-the-html-web-page/30031/2 "2024-04-10T12:00:29Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![gayathri-kannanv](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/gayathri-kannanv/32/20510_2.png) [@gayathri-kannanv](https://forum.djangoproject.com/u/gayathri-kannanv)
#### Post date: [April 10, 2024, 1:57pm UTC](https://forum.djangoproject.com/t/send-and-receive-socket-without-opening-the-html-web-page/30031/3 "2024-04-10T13:57:52Z")

</div>

chat.html

```auto
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

```auto
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

```auto
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.

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [April 10, 2024, 2:07pm UTC](https://forum.djangoproject.com/t/send-and-receive-socket-without-opening-the-html-web-page/30031/4 "2024-04-10T14:07:38Z")

</div>

> [@gayathri-kannanv](#):
>
> This is the functionality I am trying. Hope it’s understandable.

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?
