{"id":20655688,"url":"https://github.com/remast/spring-websocket-turbo","last_synced_at":"2025-10-24T09:52:00.938Z","repository":{"id":145093749,"uuid":"325956664","full_name":"remast/spring-websocket-turbo","owner":"remast","description":"Simple example for using Turbos Streams in Spring Boot. ","archived":false,"fork":false,"pushed_at":"2024-11-07T15:12:42.000Z","size":92,"stargazers_count":5,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-07T16:24:41.402Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","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/remast.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":"2021-01-01T10:19:00.000Z","updated_at":"2024-11-07T15:13:54.000Z","dependencies_parsed_at":"2024-01-20T11:24:05.469Z","dependency_job_id":"528da08b-9643-4e71-bd6c-93e35a31e96a","html_url":"https://github.com/remast/spring-websocket-turbo","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/remast%2Fspring-websocket-turbo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remast%2Fspring-websocket-turbo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remast%2Fspring-websocket-turbo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remast%2Fspring-websocket-turbo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/remast","download_url":"https://codeload.github.com/remast/spring-websocket-turbo/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224951662,"owners_count":17397425,"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-11-16T18:12:13.748Z","updated_at":"2025-10-24T09:51:55.916Z","avatar_url":"https://github.com/remast.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Spring Boot Example for TurboStreams over WebSockets\n\nSimple example for using [Turbo](https://turbo.hotwire.dev/)s [Stream](https://turbo.hotwire.dev/reference/streams)s in Go with the [Gorilla WebSocket](https://github.com/gorilla/websocket) toolkit.\n\nRun the sample using the following command:\n\n    $ ./mvnw spring-boot:run\n\nTo use the chat example, open http://localhost:8080/ in your browser.\n\n## Frontend\nThe frontend connects to the Turbo Stream using plain JavaScript like:\n\n```html\n\u003cscript src=\"https://unpkg.com/@hotwired/turbo@7.0.0-beta.1/dist/turbo.es5-umd.js\" \u003e\u003c/script\u003e\n\u003cscript\u003e\nTurbo.connectStreamSource(new WebSocket(\"ws://\" + document.location.host + \"/chat\"));\n\u003c/script\u003e\n```\n\nAfter that the frontend is connected to the Turbo Stream and get's all messages. Every chat message is appended to the dom element with id `board`.\n\nThis _should_ work with html markup too but I have not gotten it working yet.\n\n## Server\n\nThe server receives the new chat message via plain web socket. Then it wraps the message as Turbo Stream action with action `append` and broadcasts it to all subscribers. That way all subscribed users see the new message on the board.\n\nThe [SocketHandler](src/main/java/remast/websocket/turbo/SocketHandler.java) takes the incoming chat message, wraps it in a Turbo Stream Action\nand broadcasts it to all connected clients:\n```java\n    @Override\n    public void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {\n        // 1. Create identifier\n        var identifierJson = objectMapper.createObjectNode();\n        identifierJson.put(\"channel\", \"Turbo::StreamsChannel\");\n        identifierJson.put(\"signed_stream_name\", \"**mysignature**\");\n\n        // 2. Create Turbo Stream Action\n        var turboAction = \"\u003cturbo-stream action='append' target='board'\u003e\" +\n                \"\u003ctemplate\u003e\" +\n                \"\u003cp\u003e$$MESSAGE$$\u003c/p\u003e\" +\n                \"\u003c/template\u003e\" +\n                \"\u003c/turbo-stream\u003e\";\n        turboAction = turboAction.replace(\"$$MESSAGE$$\", message.getPayload());\n\n        // 3. Create WebSocket Message as JSON\n        var turboMessageJson = objectMapper.createObjectNode();\n        turboMessageJson.put(\"identifier\", objectMapper.writeValueAsString(identifierJson));\n        turboMessageJson.put(\"message\", turboAction);\n\n        // 4. Broadcast WebSocket Message to all connected sessions\n        var turboStreamPayload = objectMapper.writerWithDefaultPrettyPrinter()\n                .writeValueAsString(turboMessageJson);\n        broadcast(new TextMessage(turboStreamPayload));\n    }\n```\n\nThe raw text message sent over the web socket is:\n```json\n{ \n  \"identifier\": \n     \"{\\\"channel\\\":\\\"Turbo::StreamsChannel\\\",  \\\"signed_stream_name\\\":\\\"**mysignature**\\\"}\",\n  \"message\":\n    \"\u003cturbo-stream action='append' target='board'\u003e\n        \u003ctemplate\u003e\n            \u003cp\u003eMy new Message\u003c/p\u003e\n        \u003c/template\u003e\n    \u003c/turbo-stream\u003e\"\n}\n```\n\nTurbo Streams use raw JSON messages over WebSockets without STOMP.\n\n## Credits\nA lot of insights by https://github.com/mbucc/hotwire-demo-chat-in-springboot.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fremast%2Fspring-websocket-turbo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fremast%2Fspring-websocket-turbo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fremast%2Fspring-websocket-turbo/lists"}