I have a proxy programmed in ballerina that receives a request from a user to a web socket and sends it through another web socket to a service that processes it. Currently when the service sends me a response, the ballerina script returns that response to the user. I would like to do that once I receive the response from the service, it is sent to another service to perform a second processing and when this service responds and return it to the user.
I have the following code:
import ballerina/http;
import ballerina/log;
final string ASSOCIATED_CONNECTION = "EXTRACTOR CONNECTION";
final string EXTRACTOR = "ws://192.168.10.248:8081";
@http:WebSocketServiceConfig {
path: "/api/ws"
}
service RequestService on new http:Listener(9091) {
resource function onOpen(http:WebSocketCaller caller) {
http:WebSocketClient wsClientEp = new(
EXTRACTOR,
{callbackService: ClientService,
readyOnConnect: false,
maxFrameSize: 2147483648
});
wsClientEp.setAttribute(ASSOCIATED_CONNECTION, caller);
caller.setAttribute(ASSOCIATED_CONNECTION, wsClientEp);
var err = wsClientEp->ready();
if (err is http:WebSocketError) {
log:printError("Error calling ready on client", err);
}
}
resource function onText(http:WebSocketCaller caller, string text, boolean finalFrame) {
http:WebSocketClient clientEp = getAssociatedClientEndpoint(caller);
var err = clientEp->pushText(text, finalFrame);
if (err is http:WebSocketError) {
log:printError("Error occurred when sending text message", err);
}
}
resource function onError(http:WebSocketCaller caller, error err) {
http:WebSocketClient clientEp = getAssociatedClientEndpoint(caller);
var e = clientEp->close(statusCode = 1011, reason = "Unexpected condition");
if (e is http:WebSocketError) {
log:printError("Error occurred when closing the connection", e);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
log:printError("Unexpected error hence closing the connection", err);
}
resource function onClose(http:WebSocketCaller caller, int statusCode, string reason) {
http:WebSocketClient clientEp = getAssociatedClientEndpoint(caller);
var err = clientEp->close(statusCode = statusCode, reason = reason);
if (err is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
}
}
service ClientService = @http:WebSocketServiceConfig {} service {
resource function onText(http:WebSocketClient caller, string text, boolean finalFrame) {
http:WebSocketCaller serverEp = getAssociatedServerEndpoint(caller);
var err = serverEp->pushText(text, finalFrame);
if (err is http:WebSocketError) {
log:printError("Error occurred when sending text message", err);
}
}
resource function onError(http:WebSocketClient caller, error err) {
http:WebSocketCaller serverEp = getAssociatedServerEndpoint(caller);
var e = serverEp->close(statusCode = 1011, reason = "Unexpected condition");
if (e is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err = e);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
log:printError("Unexpected error hense closing the connection", err);
}
resource function onClose(http:WebSocketClient caller, int statusCode, string reason) {
http:WebSocketCaller serverEp = getAssociatedServerEndpoint(caller);
var err = serverEp->close(statusCode = statusCode, reason = reason);
if (err is http:WebSocketError) {
log:printError("Error occurred when closing the connection", err);
}
_ = caller.removeAttribute(ASSOCIATED_CONNECTION);
}
};
function getAssociatedClientEndpoint(http:WebSocketCaller ep) returns (http:WebSocketClient) {
http:WebSocketClient wsClient = <http:WebSocketClient>ep.getAttribute(ASSOCIATED_CONNECTION);
return wsClient;
}
function getAssociatedServerEndpoint(http:WebSocketClient ep) returns (http:WebSocketCaller) {
http:WebSocketCaller wsEndpoint = <http:WebSocketCaller>ep.getAttribute(ASSOCIATED_CONNECTION);
return wsEndpoint;
}
I have made the following pipeline: Client --> Ballerina Proxy --> Service1 --> Ballerina Proxy --> Client
I want: Client --> Ballerina Proxy --> Service1 --> Ballerina Proxy --> Service2 --> Ballerina Proxy --> Client