I want to subscribe to a specific channel of action cable. Any library or client of actioncable for android? I am using one library https://github.com/hosopy/actioncable-client-java but not able to connect with action cable.
Asked
Active
Viewed 1,380 times
0
-
Did you check if the issue is not the library but something else such as lack of credentials to access, server down, or anything else that would prevent the library to reach and interact the endpoint? – acarlstein Aug 03 '18 at 14:06
-
@acarlstein server is up and running, – Nouman Jamil Aug 03 '18 at 14:14
1 Answers
0
1.connect with the rails action cable path
private void start() {
try {
Request request = new Request.Builder().url("ws://"+Constants.HOST_ADDRESS +"/cable").addHeader("auth-token", AUTH_TOKEN).build();
EchoWebSocketListener listener = new EchoWebSocketListener();
WebSocket ws = client.newWebSocket(request, listener);
client.dispatcher().executorService().shutdown();
client = new OkHttpClient();
} catch (Exception e) {
e.printStackTrace();
}
}
2.Subscribe to the channel
private final class EchoWebSocketListener extends WebSocketListener {
@Override
public void onOpen(WebSocket webSocket, Response response) {
webSocket.send("{\"command\":\"subscribe\", \"identifier\":\"{\\\"channel\\\":\\\"CommunicationsChannel\\\"}\"}");
@Override
public void onMessage(WebSocket webSocket, String text) {
try {
JSONObject jsonBot = new JSONObject(text);
}
}
}
The WebSocket listener can receive all the message send from rails application via ‘CommunicationsChannel’, you will be able to recive it in ‘onMessage’ function
check this resource for other actionable android implementation :

errakeshpd
- 2,544
- 2
- 28
- 35