0

I have connected my web server and client its working. Now I want to call variables that's passing from backend and do a counting

for example: backend python is passing 2 variables called price of an apple and no.of apples I need to count the price of all apples and print it in client side using javascript

Any help is appreciated

  • Welcome to Stack Overflow !!! ,,, though not websockets, I think you will find what you are looking for in this post; https://stackoverflow.com/questions/12232304/how-to-implement-server-push-in-flask-framework – SteveJ Jan 01 '19 at 04:22
  • Possible duplicate of [How to implement server push in Flask framework?](https://stackoverflow.com/questions/12232304/how-to-implement-server-push-in-flask-framework) – M.R.Safari Jan 01 '19 at 07:54

1 Answers1

0

In my case I have done with this approach,

  • I have created one JS file for WebSocket event handling methods(onOpen,onClose and onMessage) which is loaded on home page loading.
  • In this file I have created one reference variable to hold events of WebSockets using (.prototype)
  • Same like second step created two seperate methods for creating connection which is going to be called from home page and one connect method to connect the server
  • In onMessage you can call your business method where you can put your logic

code look like,

var _wObj = null;
function WSSocket(tObj) {
    //basic properties
}

create connection

WSSocket.createConnection = function ( tObj ) {
    if (!_wObj) {
        _wObj = new WSSocket(tObj);
    }
    return _wObj;
}

connect to server

WSSocket.prototype.connect = function () {
    //connection logic
}

register events

WSSocket.prototype.registerEvents = function () {
        //registering logic
        this.wObj.onmessage = function (event) {
             //call your operation method
        }
}
TheSagya
  • 35
  • 11