Laravel Echo Android client.
In your build.gradle
paste the code bellow :
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
And in dependencies
:
dependencies {
compile 'com.github.MrBin99:LaravelEchoAndroid:{latest-version}'
}
// Setup options
EchoOptions options = new EchoOptions();
// Setup host of your Laravel Echo Server
options.host = "http://{my-host}:{my-port}";
/*
* Add headers for authorizing your users (private and presence channels).
* This line can change matching how you have configured
* your guards on your Laravel application
*/
options.headers.put("Authorization", "Bearer {token}");
// Create the client
Echo echo = new Echo(options);
echo.connect(new EchoCallback() {
@Override
public void call(Object... args) {
// Success connect
}
}, new EchoCallback() {
@Override
public void call(Object... args) {
// Error connect
}
});
Don't forget to close your Echo client when you've done with it or when the user kill the app. Warning: closing the socket means that you wil unsubscribe to all channels, and you will quit all presence channel you're currently in.
@Override
public void onDestroy() {
super.onDestroy();
echo.disconnect();
}
For subscribing on public channel (which means no authentication is needed) and listening for an event :
echo.channel("channel-name")
.listen("EventName", new EchoCallback() {
@Override
public void call(Object... args) {
// Event thrown.
}
});
For subscribing on private channel, this means that you need to be authenticated with headers and have the right to enter this channel :
SocketIOPrivateChannel privateChannel = echo.privateChannel("channel-name");
privateChannel.listen("EventName", new EchoCallback() {
@Override
public void call(Object... args) {
// Event thrown.
}
});
If you want to send a private message to only one member of this private channel :
try {
JSONObject data = new JSONObject();
data.put("name", "john-doe");
privateChannel.whisper("hello", data, new EchoCallback() {
@Override
public void call(Object... args) {
// Whisper received
}
});
} catch (Exception e) {
e.printStackTrace();
}
And on the other side :
privateChannel.listenForWhisper("hello", new EchoCallback() {
@Override
public void call(Object... args) {
// Received !
}
});
To enter a presence, like a private channel, you must be authenticated and have the rights.
SocketIOPresenceChannel presenceChannel = echo.presenceChannel("presence-channel");
You can now listen to particular events on presence channels :
presenceChannel.here(new EchoCallback() {
@Override
public void call(Object... args) {
// Gets users present in this channel.
// Called just after connecting to it.
}
});
presenceChannel.joining(new EchoCallback() {
@Override
public void call(Object... args) {
// Called when new user join the channel.
}
});
presenceChannel.leaving(new EchoCallback() {
@Override
public void call(Object... args) {
// Called when a user leave the channel
}
});
For leaving a channel and don't receive events related to them anymore :
echo.leave("channel-name");
For presence channel, this action make Laravel Echo Server send a "leaving" event to all other clients connected to this channel.
You can also listen for generic Socket.io events :
echo.on(Socket.EVENT_ERROR, new EchoCallback() {
@Override
public void call(Object... args) {
// Callback
}
});
You can check all available predefined events on the io.socket.client.Socket
class or on https://stackoverflow.com/questions/24224287/list-of-socket-io-events.
If you want to delete the callback :
echo.off(Socket.EVENT_ERROR);
For more information, please visit the Laravel Broadcasting documentation.
Thanks for this library https://github.com/socketio/socket.io-client-java which is a wrapper of Socket.IO in Java.