{"id":15638142,"url":"https://github.com/sacoo7/socketcluster-client-java","last_synced_at":"2025-10-13T21:36:22.151Z","repository":{"id":44869754,"uuid":"73226158","full_name":"sacOO7/socketcluster-client-java","owner":"sacOO7","description":"Native java and android client for socketcluster framework in node.js ","archived":false,"fork":false,"pushed_at":"2023-01-15T20:28:55.000Z","size":1592,"stargazers_count":94,"open_issues_count":29,"forks_count":44,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-14T19:08:49.215Z","etag":null,"topics":["android-client","android-demo","java","socketcluster"],"latest_commit_sha":null,"homepage":"http://socketcluster.io/","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sacOO7.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-08T20:54:24.000Z","updated_at":"2025-02-04T19:23:13.000Z","dependencies_parsed_at":"2023-02-09T23:30:30.122Z","dependency_job_id":null,"html_url":"https://github.com/sacOO7/socketcluster-client-java","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sacOO7%2Fsocketcluster-client-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sacOO7%2Fsocketcluster-client-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sacOO7%2Fsocketcluster-client-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sacOO7%2Fsocketcluster-client-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sacOO7","download_url":"https://codeload.github.com/sacOO7/socketcluster-client-java/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248943456,"owners_count":21186958,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android-client","android-demo","java","socketcluster"],"created_at":"2024-10-03T11:19:19.987Z","updated_at":"2025-10-13T21:36:17.118Z","avatar_url":"https://github.com/sacOO7.png","language":"Java","readme":"Java and Android Socketcluster Client\n=====================================\n\nOverview\n--------\nThis client provides following functionality\n\n- Support for emitting and listening to remote events\n- Automatic reconnection\n- Pub/sub\n- Authentication (JWT)\n\nLicense\n-------\nApache License, Version 2.0\n\nGradle\n------\nFor java \n\n```Gradle\ndependencies {\n    compile 'io.github.sac:SocketclusterClientJava:2.0.0'\n}\n```\nfor sample java examples visit [Java Demo](https://github.com/sacOO7/socketcluster-client-testing/tree/master/src/main/java)\n\nFor android \n\n```Gradle\ncompile ('io.github.sac:SocketclusterClientJava:2.0.0'){\n        exclude group :'org.json', module: 'json'\n}\n```\nfor sample android demo visit [Android Demo](https://github.com/sacOO7/socketcluster-android-demo)\n\n- In case you face any problem while resolving dependencies, you can download jar from here https://dl.bintray.com/sacoo7/Maven/io/github/sac/SocketclusterClientJava/. \n- Please report for the issue here https://github.com/sacOO7/socketcluster-client-java/issues/36\n\n[ ![Download](https://api.bintray.com/packages/sacoo7/Maven/socketcluster-client/images/download.svg) ](https://bintray.com/sacoo7/Maven/socketcluster-client/_latestVersion)\n\n\u003c!---\nDownload [latest jar dependency](https://github.com/sacOO7/socketcluster-client-java/blob/master/out/artifacts/SocketclusterClientJava_main_jar/SocketclusterClientJava_main.jar?raw=true)\n--\u003e\n\nDescription\n-----------\nCreate instance of `Socket` class by passing url of socketcluster-server end-point\n\n```java\n    //Create a socket instance\n    String url=\"ws://localhost:8000/socketcluster/\";\n    Socket socket = new Socket(url);\n     \n```\n**Important Note** : Default url to socketcluster end-point is always *ws://somedomainname.com/socketcluster/*.\n\n\n#### Registering basic listeners\n \nImplemented using `BasicListener` interface\n\n```java\n        socket.setListener(new BasicListener() {\n        \n            public void onConnected(Socket socket,Map\u003cString, List\u003cString\u003e\u003e headers) {\n                System.out.println(\"Connected to endpoint\");\n            }\n\n            public void onDisconnected(Socket socket,WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) {\n                System.out.println(\"Disconnected from end-point\");\n            }\n\n            public void onConnectError(Socket socket,WebSocketException exception) {\n                System.out.println(\"Got connect error \"+ exception);\n            }\n\n            public void onSetAuthToken(String token, Socket socket) {\n                System.out.println(\"Token is \"+ token);\n            }\n\n            public void onAuthentication(Socket socket,Boolean status) {\n                if (status) {\n                    System.out.println(\"socket is authenticated\");\n                } else {\n                    System.out.println(\"Authentication is required (optional)\");\n                }\n            }\n\n        });\n```\n\n#### Connecting to server\n\n- For connecting to server:\n\n```java\n    //This will send websocket handshake request to socketcluster-server\n    socket.connect();\n```\n\n- For connecting asynchronously to server:\n\n```java\n    //This will send websocket handshake request to socketcluster-server\n    socket.connectAsync();\n```\n\n\n- By default reconnection to server is not enabled , to enable it :\n\n```java\n    //This will set automatic-reconnection to server with delay of 2 seconds and repeating it for 30 times\n    socket.setReconnection(new ReconnectStrategy().setDelay(2000).setMaxAttempts(30));\n    socket.connect();\n```\n\n- To disable reconnection :\n\n```\n   socket.setReconnection(null); \n```\n\n- By default logging of messages is enabled ,to disable :\n\n```\n   socket.disableLogging();\n```\n\nEmitting and listening to events\n--------------------------------\n#### Event emitter\n\n- eventname is name of event and message can be String, boolean, Long or JSON-object\n\n```java\n    socket.emit(eventname,message);\n    \n    //socket.emit(\"chat\",\"Hi\");\n```\n\n- To send event with acknowledgement\n\n```java\n    socket.emit(eventname, message, new Ack() {\n                public void call(String eventName,Object error, Object data) {\n                    //If error and data is String\n                    System.out.println(\"Got message for :\"+eventName+\" error is :\"+error+\" data is :\"+data);\n                }\n        });\n```\n\n#### Event Listener\n\n- For listening to events :\n\nThe object received can be String, Boolean, Long or JSONObject.\n\n```java\n    socket.on(eventname, new Emitter.Listener() {\n                public void call(String eventName,Object object) {\n                    \n                    // Cast object to its proper datatype\n                    System.out.println(\"Got message for :\"+eventName+\" data is :\"+data);\n                }\n        }); \n```\n\n- To send acknowledgement back to server\n\n```java\n    socket.on(eventname, new Emitter.AckListener() {\n            public void call(String eventName,Object object, Ack ack) {\n                \n                // Cast object to its proper datatype                     \n                System.out.println(\"Got message :: \" + object);\n                /...\n                    Some logic goes here\n                .../\n                if (error){\n                \n                ack.call(eventName,error,null);\n                \n                }else{\n                \n                //Data can be of any data type\n                \n                ack.call(eventName,null,data);\n                }\n                \n                //Both error and data can be sent to server\n                \n                ack.call(eventName,error,data);\n            }\n        });\n        \n```\n\nImplementing Pub-Sub via channels\n---------------------------------\n\n#### Creating channel\n\n- For creating and subscribing to channels:\n\n```java\n    Socket.Channel channel = socket.createChannel(channelName);\n    //Socket.Channel channel = socket.createChannel(\"yolo\"); \n    \n    \n    /**\n     * without acknowledgement\n     */\n     channel.subscribe();\n     \n    /**\n     * with acknowledgement\n     */\n     \n    channel.subscribe(new Ack() {\n                public void call(String channelName, Object error, Object data) {\n                    if (error == null) {\n                        System.out.println(\"Subscribed to channel \"+channelName+\" successfully\");\n                    }\n                }\n        });\n```\n\n- For getting list of created channels :\n \n```java\n    List \u003cSocket.Channel\u003e channels=socket.getChannels();\n``` \n\n- To get channel by name :\n\n```java\n        Socket.Channel channel=socket.getChannelByName(\"yolo\");\n        //Returns null if channel of given name is not present\n        \n```\n\n\n\n\n#### Publishing event on channel\n\n- For publishing event :\n\n```java\n       // message can have any data type\n    /**\n     * without acknowledgement\n     */\n     channel.publish(message);\n     \n    /**\n     * with acknowledgement\n     */\n       channel.publish(message, new Ack() {\n                public void call(String channelName,Object error, Object data) {\n                    if (error == null) {\n                        System.out.println(\"Published message to channel \"+channelName+\" successfully\");\n                    }\n                }\n        });\n        \n``` \n \n#### Listening to channel\n\n- For listening to channel event :\n\n```java\n    channel.onMessage(new Emitter.Listener() {\n             public void call(String channelName , Object object) {\n                \n                 System.out.println(\"Got message for channel \"+channelName+\" data is \"+data);\n                 \n             }\n         });\n``` \n \n\u003c!--###### Pub-sub without creating channel--\u003e\n#### Un-subscribing to channel\n\n```java\n\n    /**\n     * without acknowledgement\n     */\n     \n     channel.unsubscribe();\n     \n    /**\n     * with acknowledgement\n     */\n     \n    channel.unsubscribe(new Ack() {\n                public void call(String channelName, Object error, Object data) {\n                    if (error == null) {\n                        System.out.println(\"channel unsubscribed successfully\");\n                    }\n                }\n        });    \n```\n \n \n#### Handling logging \n- Once logger object is received, it is very easy to set internal logging level of library, applying handler for each log messages.\n- It can be received using following code\n\n```java\n    Logger logger = socket.getLogger();\n```\n \n- More documentation on handling logging in custom way can be found here \n http://www.vogella.com/tutorials/Logging/article.html\n \n#### Handling SSL connection with server\n \n`WebSocketFactory` class is responsible for creating websocket instances and handling settings with server, for more\ninformation visit [ here ](https://github.com/TakahikoKawasaki/nv-websocket-client/blob/master/README.md)\n\nTo get instance of `WebSocketFactory` class :\n\n```java\n   \n    WebSocketFactory factory=socket.getFactorySettings();\n    \n```\n \nThe following is an example to set a custom SSL context to a `WebSocketFactory`\ninstance. (Again, you don't have to call a `setSSL*` method if you use the default\nSSL configuration.)\n\n```java\n// Create a custom SSL context.\nSSLContext context = NaiveSSLContext.getInstance(\"TLS\");\n\n// Set the custom SSL context.\nfactory.setSSLContext(context);\n```\n\n[NaiveSSLContext](https://gist.github.com/TakahikoKawasaki/d07de2218b4b81bf65ac)\nused in the above example is a factory class to create an `SSLContext` which\nnaively accepts all certificates without verification. It's enough for testing\npurposes. When you see an error message \"unable to find valid certificate path\nto requested target\" while testing, try `NaiveSSLContext`. \n \n \n#### Setting HTTP proxy with server\n \nIf a WebSocket endpoint needs to be accessed via an HTTP proxy, information\nabout the proxy server has to be set to a `WebSocketFactory` instance before\ncreating a `WebSocket` instance. Proxy settings are represented by\n`ProxySettings` class. A `WebSocketFactory` instance has an associated\n`ProxySettings` instance and it can be obtained by calling\n`WebSocketFactory.getProxySettings()` method.\n\n```java\n// Get the associated ProxySettings instance.\nProxySettings settings = factory.getProxySettings();\n```\n\n`ProxySettings` class has methods to set information about a proxy server such\nas `setHost` method and `setPort` method. The following is an example to set a\nsecure (`https`) proxy server.\n\n```java\n// Set a proxy server.\nsettings.setServer(\"https://proxy.example.com\");\n```\n\nIf credentials are required for authentication at a proxy server, `setId`\nmethod and `setPassword` method, or `setCredentials` method can be used to set\nthe credentials. Note that, however, the current implementation supports only\nBasic Authentication.\n\n```java\n// Set credentials for authentication at a proxy server.\nsettings.setCredentials(id, password);\n``` \n#### Star the repo. if you love the client :).\n \n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsacoo7%2Fsocketcluster-client-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsacoo7%2Fsocketcluster-client-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsacoo7%2Fsocketcluster-client-java/lists"}