ofxJSONRPC and callbacks


#1

Hi, I am using ofxJSONRPC, it is really fantastic, but I wanted to know if there is a way to have a callback in the server side. At the moment in the example, I get callback methods in the c++ OF side. I am currently polling from the javascript every second to look for changes in the config. I would prefer to have a callback so I don’t have to poll like this.
Maybe @bakercp knows this?

Cheers


#2

Hey @fresla are you trying to do “push” notifications to the browser from an OF server?

You should be able to access the websocket route (a member of the ofxJSONRPCServer) via

server.webSocketRoute().broadcast(your_frame_to_broadcast);

Since the example client js library is intended to parse requests rather than pushed data, you will have to take a look and see how to handle websocket frames that it didn’t request.

If you don’t want to broadcast to all connected clients, you can keep track of new ofxHTTP::WebSocketConnection by subscribing to the WebSocketRoute events and sending messages directly using the connection’s ofxHTTP::WebSocketConnection::sendFrame(…) method. The classes are all documented pretty extensively inline, but I don’t have an example of that right now (though it is all implemented under a few layers in ofxJSONRPC server).

Alternatively, you might also take a look at the server sent events route in ofxHTTP would work for you. But if you are already using websockets, you don’t really need to do that.