{"id":24255,"date":"2024-03-16T02:49:23","date_gmt":"2024-03-16T02:49:23","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/"},"modified":"2024-03-22T03:15:59","modified_gmt":"2024-03-22T03:15:59","slug":"how-can-a-java-server-proactively-push-data","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/","title":{"rendered":"How can a Java server proactively push data?"},"content":{"rendered":"<p>Java server can utilize WebSocket for pushing data actively. WebSocket is a protocol that enables full-duplex communication over a single TCP connection, allowing real-time data exchange between clients and servers.<\/p>\n<p>Firstly, you need to create a WebSocket server on a Java server. You can use the Java WebSocket API in the Java library to achieve this. Below is a simple example:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">import<\/span> javax.websocket.*;\r\n<span class=\"hljs-keyword\">import<\/span> javax.websocket.server.ServerEndpoint;\r\n<span class=\"hljs-keyword\">import<\/span> java.io.IOException;\r\n\r\n<span class=\"hljs-meta\">@ServerEndpoint(\"\/websocket\")<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">WebSocketServer<\/span> {\r\n\r\n    <span class=\"hljs-meta\">@OnOpen<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title function_\">onOpen<\/span><span class=\"hljs-params\">(Session session)<\/span> {\r\n        System.out.println(<span class=\"hljs-string\">\"WebSocket opened: \"<\/span> + session.getId());\r\n    }\r\n\r\n    <span class=\"hljs-meta\">@OnMessage<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title function_\">onMessage<\/span><span class=\"hljs-params\">(String message, Session session)<\/span> <span class=\"hljs-keyword\">throws<\/span> IOException {\r\n        System.out.println(<span class=\"hljs-string\">\"Received message from client: \"<\/span> + message);\r\n        session.getBasicRemote().sendText(<span class=\"hljs-string\">\"Hello client!\"<\/span>);\r\n    }\r\n\r\n    <span class=\"hljs-meta\">@OnClose<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title function_\">onClose<\/span><span class=\"hljs-params\">(Session session)<\/span> {\r\n        System.out.println(<span class=\"hljs-string\">\"WebSocket closed: \"<\/span> + session.getId());\r\n    }\r\n\r\n    <span class=\"hljs-meta\">@OnError<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title function_\">onError<\/span><span class=\"hljs-params\">(Throwable error)<\/span> {\r\n        System.err.println(<span class=\"hljs-string\">\"WebSocket error: \"<\/span> + error.getMessage());\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>You can then communicate with the server using the WebSocket API in JavaScript or other languages in the client. Send messages to the server via WebSocket and receive messages from the server.<\/p>\n<p>Here is a simple example using JavaScript:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">var<\/span> socket = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">WebSocket<\/span>(<span class=\"hljs-string\">\"ws:\/\/localhost:8080\/websocket\"<\/span>);\r\n\r\nsocket.<span class=\"hljs-property\">onopen<\/span> = <span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>) {\r\n    <span class=\"hljs-variable language_\">console<\/span>.<span class=\"hljs-title function_\">log<\/span>(<span class=\"hljs-string\">\"WebSocket opened\"<\/span>);\r\n    socket.<span class=\"hljs-title function_\">send<\/span>(<span class=\"hljs-string\">\"Hello server!\"<\/span>);\r\n};\r\n\r\nsocket.<span class=\"hljs-property\">onmessage<\/span> = <span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\">event<\/span>) {\r\n    <span class=\"hljs-variable language_\">console<\/span>.<span class=\"hljs-title function_\">log<\/span>(<span class=\"hljs-string\">\"Received message from server: \"<\/span> + event.<span class=\"hljs-property\">data<\/span>);\r\n};\r\n\r\nsocket.<span class=\"hljs-property\">onclose<\/span> = <span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>) {\r\n    <span class=\"hljs-variable language_\">console<\/span>.<span class=\"hljs-title function_\">log<\/span>(<span class=\"hljs-string\">\"WebSocket closed\"<\/span>);\r\n};\r\n\r\nsocket.<span class=\"hljs-property\">onerror<\/span> = <span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\">error<\/span>) {\r\n    <span class=\"hljs-variable language_\">console<\/span>.<span class=\"hljs-title function_\">error<\/span>(<span class=\"hljs-string\">\"WebSocket error: \"<\/span> + error);\r\n};\r\n<\/code><\/pre>\n<p>In the examples mentioned above, a message is sent to the server when the WebSocket connection is successfully established. Upon receiving the message, the server will send a reply back to the client.<\/p>\n<p>In this way, the server can actively push data to the client. You can use session.getBasicRemote().sendText() method to send messages to the client from anywhere on the server side.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java server can utilize WebSocket for pushing data actively. WebSocket is a protocol that enables full-duplex communication over a single TCP connection, allowing real-time data exchange between clients and servers. Firstly, you need to create a WebSocket server on a Java server. You can use the Java WebSocket API in the Java library to achieve [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-24255","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How can a Java server proactively push data? - Blog - Silicon Cloud<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How can a Java server proactively push data?\" \/>\n<meta property=\"og:description\" content=\"Java server can utilize WebSocket for pushing data actively. WebSocket is a protocol that enables full-duplex communication over a single TCP connection, allowing real-time data exchange between clients and servers. Firstly, you need to create a WebSocket server on a Java server. You can use the Java WebSocket API in the Java library to achieve [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-16T02:49:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-22T03:15:59+00:00\" \/>\n<meta name=\"author\" content=\"Olivia Parker\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Olivia Parker\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/\"},\"author\":{\"name\":\"Olivia Parker\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9\"},\"headline\":\"How can a Java server proactively push data?\",\"datePublished\":\"2024-03-16T02:49:23+00:00\",\"dateModified\":\"2024-03-22T03:15:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/\"},\"wordCount\":172,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/\",\"name\":\"How can a Java server proactively push data? - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-16T02:49:23+00:00\",\"dateModified\":\"2024-03-22T03:15:59+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How can a Java server proactively push data?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9\",\"name\":\"Olivia Parker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g\",\"caption\":\"Olivia Parker\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/oliviaparker\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How can a Java server proactively push data? - Blog - Silicon Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/","og_locale":"en_US","og_type":"article","og_title":"How can a Java server proactively push data?","og_description":"Java server can utilize WebSocket for pushing data actively. WebSocket is a protocol that enables full-duplex communication over a single TCP connection, allowing real-time data exchange between clients and servers. Firstly, you need to create a WebSocket server on a Java server. You can use the Java WebSocket API in the Java library to achieve [&hellip;]","og_url":"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-16T02:49:23+00:00","article_modified_time":"2024-03-22T03:15:59+00:00","author":"Olivia Parker","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Olivia Parker","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/"},"author":{"name":"Olivia Parker","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9"},"headline":"How can a Java server proactively push data?","datePublished":"2024-03-16T02:49:23+00:00","dateModified":"2024-03-22T03:15:59+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/"},"wordCount":172,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/","url":"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/","name":"How can a Java server proactively push data? - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-16T02:49:23+00:00","dateModified":"2024-03-22T03:15:59+00:00","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-can-a-java-server-proactively-push-data\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How can a Java server proactively push data?"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9","name":"Olivia Parker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g","caption":"Olivia Parker"},"url":"https:\/\/www.silicloud.com\/blog\/author\/oliviaparker\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/24255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=24255"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/24255\/revisions"}],"predecessor-version":[{"id":58277,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/24255\/revisions\/58277"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=24255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=24255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=24255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}