Pusher Client under Akka's actor context.
The working sample with Play Framework is available here.
- Java 8 or higher
- Scala 2.10 and Scala 2.11
Add the following to your sbt build (Scala 2.11.x):
libraryDependencies += "com.github.dtaniwaki" %% "akka-pusher" % "x.y.z"
Here, x.y.z
is the akka-pusher package version you want to use.
import com.github.dtaniwaki.akka_pusher.PusherClient
implicit val system = ActorSystem("pusher")
val pusher = new PusherClient()
pusher.trigger("test_channel", "my_event", "hello world")
pusher.shutdown()
If you want to run another actor for pusher,
import com.github.dtaniwaki.akka_pusher.PusherActor
import com.github.dtaniwaki.akka_pusher.PusherMessages._
val system = ActorSystem("pusher")
val pusherActor = system.actorOf(PusherActor.props(), "pusher-actor")
pusherActor ! TriggerMessage("test_channel", "my_event", "hello world")
val result: Future[Try[Result]] = pusher.trigger("test_channel", "my_event", Map("foo" -> "bar"))
val result: Future[Try[Result]] = pusher.trigger(Seq(("test_channel", "my_event", Map("foo" -> "bar"))))
val channels: Future[Try[ChannelMap]] = pusher.channels("presence-my_", Seq(PusherChannelsAttributes.userCount))
val channel: Future[Try[Channel]] = pusher.channel("presence-my_channel", Seq(PusherChannelAttributes.userCount))
val users: Future[Try[UserList]] = pusher.users("presence-my_channel")
case class Foo(body: String)
implicit val FooJsonSupport = jsonFormat1(Foo)
val channelData: ChannelData[Foo] = ChannelData("user_id", Foo("body"))
val params: AuthenticatedParams = authenticate("my_channel", "socket_id", Some(channelData))
val valid: Signature = validateSignature("pusher_key", "pusher_signature", "body")
(pusherActor ask TriggerMessage("channel-name", "event-name", "JSON OBJECT".toJson, Some("123.345"))).map {
case Success(res: PusherModels.Result) => println(res)
case Failure(e) => throw e
}
If the batchTrigger
setting of PusherActor
is true
,
pusherActor ! TriggerMessage(channel, event, body.toJson, socketId)
The trigger will be executed in batch in 1000 milliseconds (default).
(pusherActor ask ChannelMessage("presence-my_channel", Seq(PusherChannelAttributes.userCount))).map {
case Success(res: PusherModels.Channel) => println(res)
case Failure(e) => throw e
}
(pusherActor ask ChannelsMessage("presence-my_", Seq(PusherChannelsAttributes.userCount))).map {
case Success(res: ChannelMap) =>
println(res)
case Failure(e) => throw e
}
(pusherActor ask UsersMessage("presence-my_channel")).map {
case Success(res: UserList) =>
println(res)
case Failure(e) => throw e
}
val pusherRequest = AuthRequest()
(pusherActor ask AuthenticateMessage(
"channel-name",
Some("123.345"),
Some(PusherModels.ChannelData(userId = "dtaniwaki", userInfo = Some(Map("user_name" -> "dtaniwaki", "name" -> "Daisuke Taniwaki").toJson)))
)).map {
case res: PusherModels.AuthenticatedParams =>
println(res)
}
(pusherActor ask ValidateSignatureMessage(key, signature, request.body.toString)).map {
case Success(res) =>
println(res)
case Failure(e) =>
throw e
}
PusherClient use pusher
scope in application.conf
parsed by typesafe config.
pusher {
appId=${?PUSHER_APP_ID}
key=${?PUHSER_API_KEY}
secret=${?PUSHER_API_SECRET}
batchTrigger=true
batchInterval=1000
}
Here, you can replace the variables or set them as environment variables.
Or, you can directly set the config by the costructor argument.
val pusher = new PusherClient(ConfigFactory.parseString("""pusher: {appId: "app0", key: "key0", secret: "secret0"}"""))
key | type | description |
---|---|---|
appId |
String |
Your pusher app ID. |
key |
String |
Your pusher app key. |
secret |
String |
Your pusher app secret. |
ssl |
Boolean (default: false) |
Encrypt API request with SSL |
key | type | description |
---|---|---|
batchTrigger |
Boolean (default: false) |
Flag to enable batch trigger requests. The batch size is 100 as pusher limits it. |
batchInterval |
Int (default: 1000) |
Milliseconds to make batch trigger requests. |
sbt test
sbt clean coverage test
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Copyright (c) 2015 Daisuke Taniwaki. See LICENSE for details.