A Java client compatible with Laravel Echo Server
Laravel jEcho Client uses the java-based socket.io-client library developed by nkzawa to easily interface your Java application to a Laravel Echo Server.
- Socket.IO
Since this library works as a wrapper, it's mandatory to properly configure your Laravel environment. Keep in mind that this library works only using the auth:api guard, as we need to pass the api key to let it work. You can follow these links as long as they can help you with the configuration:
Once you have a fully working Laravel Echo environment, you can proceed with the next steps.
Additional information on broadcasting with Laravel can be found on the official docs: https://laravel.com/docs/master/broadcasting
We need to include the Laravel jEcho Client as a library inside our project
- Add the JitPack repository to your build file
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- Add the dependency
dependencies {
implementation 'com.github.AngeloAvv:laravel-jecho-client:0.1.1'
}
- Add the JitPack repository to your build file
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
- Add the dependency
<dependency>
<groupId>com.github.AngeloAvv</groupId>
<artifactId>laravel-jecho-client</artifactId>
<version>0.1.1</version>
</dependency>
Next you need to create an Echo instance as it follows:
Options options = new Options();
options.url = "http://localhost:6001";
options.namespace = "App.Events";
options.token = "apiToken";
Echo instance = new Echo(options);
When you create a new Echo instance, the client tries to connect to the Socket.IO server. In this phase your application must be able to connect to the network.
You can setup your Echo instance to listen for a specific event registered for a specific channel using the listen method.
instance.listen("channel-name", "event", new Channel.OnEvent<JSONObject>() {
public void onEvent(JSONObject arg) {
}
});
The library is able to satisfy almost all of the Laravel Echo behaviors. You can join to private, presence and public channels. The code looks the same as the js implementation.
instance.privateChannel("channel-name")
.listen("event", new Channel.OnEvent<JSONObject>() {
@Override
public void onEvent(JSONObject arg) {
}
});
instance.join("channel-name")
.here(new PresenceChannel.OnUsersHereListener() {
@Override
public void onUsersHere(int... ids) {
}
})
.joining(new PresenceChannel.OnUserJoiningListener() {
@Override
public void onUserJoining(int id) {
}
})
.leaving(new PresenceChannel.OnUserLeavingListener() {
@Override
public void onUserLeaving(int id) {
}
});
instance.channel("channel-name")
.listen("event", new Channel.OnEvent<JSONObject>() {
@Override
public void onEvent(JSONObject arg) {
}
});
The event listener supports Java Generics, so you can switch between JSONObject and JSONArray class types to satisfy your needs.
When you're done, remember to leave the channel using the following syntax:
instance.leave("channel-name");
Since Socket.IO is based on WebSockets, you need to close the connection when everything is done.
instance.disconnect();
- Pusher integration
- JSON event payload deserialization through Gson, Jackson or Moshi
Android shoud be fully supported. This library is still a work-in-progress. Please feel free to submit new issues if you encounter some problems with it.
Laravel jEcho Client is available under the MIT license. See the LICENSE file for more info.