I can send and receive messages from server on websockets. I need to write a function that will send data to the server and is awaiting a response from the server and then r
WebSocket is like TCP, it's just a transport layer. What you need is to run a protocol on top of webSocket. The protocol takes care of creating the correlations between messages for you. You may want to take a look at AMQP, which is the most extensive and complete open protocol for real time asynchronous messaging. See for example https://github.com/dansimpson/amqp-js for a simple AMQP client JS library.
I don't know what type of server you are using, but AMQP will give you the most flexibility as there is a lot of support for AMQP across many platforms. There are also simple AMQP message broker implementations. If your server is Java, then you will want to look at JMS. in which case you will need a more elaborate but robust JMS broker.