Quark Engine - Channel selection
In the Quark Engine Web Demo app, one could see examples for exposing Java classes through HTTP/s and WebSocket channels. Now, the question is how to use only HTTP/s channels or only WebSocket channels. In this short tutorial we will show how.
Quark Engine contains 2 main endpoint types:
- io.greenscreens.web.APIServlet - HTTP/s channel
- io.greenscreens.websocket.WebSocketService - WebSocket channel
APIServlet uses Http GET method to return JSON structure describing exposed Java Classes and their methods while Http POST is used to handle API calls from browser.
WebSocketService handle API calls and automatic API registration if both "api" and "service" URLs are the same.
When using only HTTP/s channels, simply do not define WebSocket service inside the Java Web App module. And when using WebSocket channel, do not define servlet based on APIServlet class. And that's all that is required on the server side. If you want to use them both, sure, no problem also.
From the browser side few combinations are possible.
API and service through HTTP/s channel
let url = 'http://localhost/demo/api';
await Engine.init({api: url, service: url}):
API and service through WebSocket channel
let url = 'ws://localhost/demo/socket';
await Engine.init({api: url, service: url});
API through HTTP/s channel and service through WebSocket channel
let api = 'http://localhost/demo/api';
let ws = 'ws://localhost/demo/socket';
await Engine.init({api: api, service: ws});
Which one to use?
Well, we would prefer WebSocket only approach. In a case when there are technical problems as some network security devices might not be configured properly for WebSocket or they are blocking WebSocket for some reason, standard HTTP approach is the only solution.
Main difference is that WebSocket is an open active channel, while http/https makes connection handshake every time making responses a little bit slower, and making a little bit more pressure on the server side.