{"id":17956100,"url":"https://github.com/sleipnir/kotlin-session-manager-example","last_synced_at":"2025-04-03T17:18:27.024Z","repository":{"id":138952472,"uuid":"243334011","full_name":"sleipnir/kotlin-session-manager-example","owner":"sleipnir","description":null,"archived":false,"fork":false,"pushed_at":"2020-02-26T18:22:39.000Z","size":185,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-09T05:44:42.432Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sleipnir.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-26T18:21:04.000Z","updated_at":"2020-02-26T18:22:41.000Z","dependencies_parsed_at":"2024-05-19T04:34:36.886Z","dependency_job_id":null,"html_url":"https://github.com/sleipnir/kotlin-session-manager-example","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/sleipnir%2Fkotlin-session-manager-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sleipnir%2Fkotlin-session-manager-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sleipnir%2Fkotlin-session-manager-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sleipnir%2Fkotlin-session-manager-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sleipnir","download_url":"https://codeload.github.com/sleipnir/kotlin-session-manager-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247043352,"owners_count":20874087,"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":[],"created_at":"2024-10-29T10:34:43.639Z","updated_at":"2025-04-03T17:18:27.007Z","avatar_url":"https://github.com/sleipnir.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Kotlin Session Manager Example\n\nBasead on Java Session Manager Example by [@domschoen](https://github.com/domschoen/cloudstate-samples-java-session-manager)\n\n# Cloudstate Session Manager\n## Description\nExample of a Session Manager which manage sessions for accounts like you may have when using a video streaming platform.\nIn such platform, depending on your subscription, you may have more or less simultaneous viewings (= sessions).\nHere we represent subscription as Home entity and the number of simultaneous viewing by the max number of sessions.\n\nWe have 2 services:\n\n| Service   | Entity    | Entity Key    |\n| --------- | --------- | ------------- |\n| SM        | Home      | Account ID    |\n| Device    | Device    | Device ID     |\n\nThose 2 services runs in the same Cloudstate user function because we want to forward from one service to the other (and this is only possible inside the same user function).\n\n### Standard use case: the device knows the account ID\nIn this standard use case, the device uses the first service \"Home\".\nFor each video the user start to watch from a device, the device needs a session. It means that the device send to the \"Home\" service the command:\n\n- CreateSession\n\nThen the device will regularly ask for session renewal with command:\n\n- HeartBeat\n\nWhen the user has finished watching a video, the device will terminate the session sending command:\n\n- TearDown\n\nNote: If a device tries to ask for an extra session above the max number of sessions of the Home account, then you will have an error.\n\n### Why do we need a Device service ?\nWhen you use the Home service you are using the account ID but we wanted to cover a special use case: The device doesn't always knows the account ID and may ask for a session with only the device ID. In that case, the session manager should use another service to get the account ID from Device ID and then proceed as usual.\n\nAt first we wanted to have a service able to represent the Account - Devices relationship i.e the Account having multiple Devices registered but what we need is this:\n- getDeviceID(deviceID) returning AccountID\n\nThat command is not possible if we represent account because the key of an account is the account ID. If you look at the command above we cannot pass the account ID because it is what we want in the response ! This command would be possible with a projection (read side) but this functionality is not available for the moment in CloudState.\n\nTo solve the problem we decided to use the Device ID as the key and have a Device service even if it not exactly what we wanted.\n\n### What happened if the device want a session knowing only the Device ID ?\nIn such case the device will not ask the Home service but the Device service for the session creation. Here the steps:\n\n1. The device ask the Device service for the session passing the device ID\n2. The \"Device\" service knows the account ID for the device ID (it should have been set beforehand to the \"Device\" service with the CreateDevice command)\n3. The \"Device\" service takes the account ID and forward the session creation command with the account ID to the \"Home\" service which will return the new session created to the device.\n\n## Run on Minikube with Cassandra\nTo run this example, you need to run some command in this project: https://github.com/cloudstateio/cloudstate\n\n1. `minikube start --vm-driver=hyperkit --memory 8192 --cpus 2`\n2. ```eval $(minikube docker-env)```\n3. ```\n   cd couldstate-master\n   sbt -Ddocker.tag=dev\n   operator/docker:publishLocal\n   dockerBuildCassandra publishLocal\n   exit\n   ```\n5. ```kubectl create namespace cloudstate```\n6. ```kubectl apply -n cloudstate -f operator/cloudstate-dev.yaml```\n7. Edit config to remove \"native-\" in images:```kubectl edit -n cloudstate configmap cloudstate-operator-config```\n8. ```cd cloudstate-samples-java-session-manager```\n9. ```kubectl apply -f descriptors/store/cassandra-store.yaml```\n10. ```kubectl apply -f descriptors/cassandra```\n11. ```sbt -Ddocker.tag=dev\n    project java-session-manager\n    docker:publishLocal\n    exit\n    ```\n12. ```kubectl apply -f descriptors/java-session-manager/java-session-manager.yaml```\n13. ```\n    kubectl expose deployment sm-deployment --port=8013 --type=NodePort\n    minikube service sm-deployment --url\n    http://192.168.64.35:30320\n    grpcurl -plaintext 192.168.64.35:30320 describe\n    ```\n14. To try it, launch Postman with for example:\n\n    |        | Value                                                  |\n    | ------ | ------------------------------------------------------ |\n    | Method | POST                                                   |\n    | URL    | http://192.168.64.35:30320/home/MyHome/sessions/create |\n    | Body   | {\u003cbr/\u003e\t\"device_id\" = \"Android Phone\"\u003cbr/\u003e}           |\n\n    and you will get session for \"MyHome\":\n\n    ```\n    {\n        \"accountId\": \"MyHome\",\n        \"sessionId\": \"1b387cfb-aabe-4b94-ae7e-0f31fc6f909b\",\n        \"expiration\": \"1575996502\"\n    }\n    ```\n\n    \n ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsleipnir%2Fkotlin-session-manager-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsleipnir%2Fkotlin-session-manager-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsleipnir%2Fkotlin-session-manager-example/lists"}