2

My web application uses javascript to communicate with a server by HTTP requests. However the server software is changed so that instead of using HTTP requests, it uses WebSocket communication.

Rewriting the the entire web application to use open communication (i.e. WebSockets) instead of a request-respond mechanism entirely will be an incredibly tedious job, and I don't really have anything to gain on using WebSockets for the web application.

What I want to be able to achieve is to add a javascript module that communicates by WebSockets, but still in a request-respond manner. I guess this means that I have to "pause" the javascript until the server responds to my WebSocket messages? Because with HTTP requests my web application waited until the request was responded to.

So I guess it boils down to: How can I make javascript wait until the server sends a WebSocket message?

Cheers

EDIT: To specify, my Javascript code uses HTTP requests throughout the source code via a function I named "get(URL)". I want to modify this function so that it sends a WebSocket message, waits for the response and returns the response. Any ideas?

9
  • If you don't see the point of using websockets, why won't you just stick to the Ajax calls, like now? Commented Jun 2, 2015 at 13:23
  • You basically want to simulate synchronous ajax requests, but with web-sockets? Commented Jun 2, 2015 at 13:25
  • Supposedly even with AJAX calls your code was asynchronous, no? You just need to replace the on-AJAX-response-invoke-callback mechanism with an on-specific-websocket-message-invoke-callback mechanism. Commented Jun 2, 2015 at 13:26
  • @RoboRobok: The server does not respond to HTTP requests like it used to. Now it only accepts WebSocket connection. Commented Jun 2, 2015 at 13:27
  • Your server doesn't respond to http? Then how do visitors view your web pages? The browsers uses HTTP. Commented Jun 2, 2015 at 13:28

1 Answer 1

2

If I understand you correctly from the comments, you do in fact not desire to "pause" your Javascript, but merely to replace async ajax calls with websocket communication.

You would emit a message to the server, with a pre-defined key as it is in the server. You could then have some sort of payload in the message if you desire, and then have a callback function with the data you receive. Following is a sample where I did something similar for a simple chat application I created.

My client code:

socket.emit('join', options, function(res){
    _printToChat(res);
});

Server code:

socket.on('join', function(roomname, fn){
    socket.join(roomname);
    fn('You joined ' + roomname);
});

This sends a messge with the key "join", and the callback is called with the response from server. In this case res in the client would be "You joined " + roomname.

You would likely do something like:

Client code:

socket.emit('getSomeResource', options, function(res){
    // do something with response in res variable.
});

Server code:

socket.on('getSomeResource', function(yourOptions, fn){
    fn(resourceToReturn);
});
Sign up to request clarification or add additional context in comments.

4 Comments

The problem is that throughout my js code, I make a lot of HTTP requests. Changing all these method calls and adding a callback for each will be too tedious. Therefore I want to “trick“ my js code to think it is sending http requests when it is in fact WebSocket communication.
That's why I need to "pause" it. The fake "request" method call must wait until a response is received and return it...
See edit in original post, I think it should be clearer what I'm trying to do
Making synchronous calls isn't really a good idea, and you should consider rewriting your code into a callback structure. However, to answer your question, writing synchronous websockets isn't easily achieved, and you'll have to look into simulating it by putting the callbacks into a queue of sorts, perhaps look into something like Ronan Dejhero's answer in this post. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.