{"id":24248,"date":"2024-03-16T02:48:50","date_gmt":"2024-03-16T02:48:50","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/"},"modified":"2024-03-22T03:14:56","modified_gmt":"2024-03-22T03:14:56","slug":"how-to-send-messages-to-the-front-end-using-websocket","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/","title":{"rendered":"How to send messages to the front end using WebSocket?"},"content":{"rendered":"<p>Messages can be sent to the frontend using the WebSocket protocol.<\/p>\n<p>WebSocket is a bidirectional communication protocol that allows real-time data transfer between client and server by establishing a persistent connection. In the frontend, the WebSocket API in JavaScript can be used to communicate with the server.<\/p>\n<p>Here is a simple example demonstrating how to use WebSocket to push messages to the frontend.<\/p>\n<ol>\n<li>Server-side code (using Node.js and the express framework):<\/li>\n<\/ol>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">const<\/span> express = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'express'<\/span>);\r\n<span class=\"hljs-keyword\">const<\/span> app = <span class=\"hljs-title function_\">express<\/span>();\r\n<span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-title class_\">WebSocket<\/span> = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'ws'<\/span>);\r\n\r\n<span class=\"hljs-keyword\">const<\/span> wss = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">WebSocket<\/span>.<span class=\"hljs-title class_\">Server<\/span>({ <span class=\"hljs-attr\">server<\/span>: app.<span class=\"hljs-title function_\">listen<\/span>(<span class=\"hljs-number\">3000<\/span>) });\r\n\r\n<span class=\"hljs-comment\">\/\/ \u76d1\u542cWebSocket\u8fde\u63a5\u4e8b\u4ef6<\/span>\r\nwss.<span class=\"hljs-title function_\">on<\/span>(<span class=\"hljs-string\">'connection'<\/span>, <span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title function_\">connection<\/span>(<span class=\"hljs-params\">ws<\/span>) {\r\n  <span class=\"hljs-comment\">\/\/ \u76d1\u542c\u524d\u7aef\u53d1\u9001\u7684\u6d88\u606f<\/span>\r\n  ws.<span class=\"hljs-title function_\">on<\/span>(<span class=\"hljs-string\">'message'<\/span>, <span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title function_\">incoming<\/span>(<span class=\"hljs-params\">message<\/span>) {\r\n    <span class=\"hljs-variable language_\">console<\/span>.<span class=\"hljs-title function_\">log<\/span>(<span class=\"hljs-string\">'received: %s'<\/span>, message);\r\n  });\r\n\r\n  <span class=\"hljs-comment\">\/\/ \u5411\u524d\u7aef\u53d1\u9001\u6d88\u606f<\/span>\r\n  ws.<span class=\"hljs-title function_\">send<\/span>(<span class=\"hljs-string\">'Hello, client!'<\/span>);\r\n});\r\n<\/code><\/pre>\n<ol>\n<li>Front-end code.<\/li>\n<\/ol>\n<pre class=\"post-pre\"><code><span class=\"hljs-comment\">\/\/ \u521b\u5efaWebSocket\u8fde\u63a5<\/span>\r\n<span class=\"hljs-keyword\">const<\/span> socket = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">WebSocket<\/span>(<span class=\"hljs-string\">'ws:\/\/localhost:3000'<\/span>);\r\n\r\n<span class=\"hljs-comment\">\/\/ \u76d1\u542c\u8fde\u63a5\u6210\u529f\u4e8b\u4ef6<\/span>\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\">'Connected to server'<\/span>);\r\n};\r\n\r\n<span class=\"hljs-comment\">\/\/ \u76d1\u542c\u6536\u5230\u6d88\u606f\u4e8b\u4ef6<\/span>\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: '<\/span> + event.<span class=\"hljs-property\">data<\/span>);\r\n};\r\n\r\n<span class=\"hljs-comment\">\/\/ \u5411\u670d\u52a1\u5668\u53d1\u9001\u6d88\u606f<\/span>\r\nsocket.<span class=\"hljs-title function_\">send<\/span>(<span class=\"hljs-string\">'Hello, server!'<\/span>);\r\n<\/code><\/pre>\n<p>In the example above, a WebSocket server is created by the server using Node.js and the express framework, listening on port 3000. When a front-end client connects to the server, the connection event is triggered, allowing the server to use the ws object to listen for messages sent from the front-end and send messages back using the send method.<\/p>\n<p>The front end utilizes the WebSocket API in JavaScript to create a WebSocket object, and listens for both the successful connection event (onopen) and message received event (onmessage). After a successful connection, the front end can use the send method to send messages to the server and receive messages sent from the server by listening to the onmessage event.<\/p>\n<p>Using WebSocket, the server and front-end can achieve real-time two-way communication, making it easy to push messages to the front-end.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Messages can be sent to the frontend using the WebSocket protocol. WebSocket is a bidirectional communication protocol that allows real-time data transfer between client and server by establishing a persistent connection. In the frontend, the WebSocket API in JavaScript can be used to communicate with the server. Here is a simple example demonstrating how to [&hellip;]<\/p>\n","protected":false},"author":14,"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-24248","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 to send messages to the front end using WebSocket? - 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-to-send-messages-to-the-front-end-using-websocket\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to send messages to the front end using WebSocket?\" \/>\n<meta property=\"og:description\" content=\"Messages can be sent to the frontend using the WebSocket protocol. WebSocket is a bidirectional communication protocol that allows real-time data transfer between client and server by establishing a persistent connection. In the frontend, the WebSocket API in JavaScript can be used to communicate with the server. Here is a simple example demonstrating how to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/\" \/>\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:48:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-22T03:14:56+00:00\" \/>\n<meta name=\"author\" content=\"Noah Thompson\" \/>\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=\"Noah Thompson\" \/>\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-to-send-messages-to-the-front-end-using-websocket\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/\"},\"author\":{\"name\":\"Noah Thompson\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a\"},\"headline\":\"How to send messages to the front end using WebSocket?\",\"datePublished\":\"2024-03-16T02:48:50+00:00\",\"dateModified\":\"2024-03-22T03:14:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/\"},\"wordCount\":222,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/\",\"name\":\"How to send messages to the front end using WebSocket? - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-16T02:48:50+00:00\",\"dateModified\":\"2024-03-22T03:14:56+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to send messages to the front end using WebSocket?\"}]},{\"@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\/2e83cc6ab9f60d36921c2d0f9f280f4a\",\"name\":\"Noah Thompson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g\",\"caption\":\"Noah Thompson\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/noahthompson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to send messages to the front end using WebSocket? - 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-to-send-messages-to-the-front-end-using-websocket\/","og_locale":"en_US","og_type":"article","og_title":"How to send messages to the front end using WebSocket?","og_description":"Messages can be sent to the frontend using the WebSocket protocol. WebSocket is a bidirectional communication protocol that allows real-time data transfer between client and server by establishing a persistent connection. In the frontend, the WebSocket API in JavaScript can be used to communicate with the server. Here is a simple example demonstrating how to [&hellip;]","og_url":"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-16T02:48:50+00:00","article_modified_time":"2024-03-22T03:14:56+00:00","author":"Noah Thompson","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Noah Thompson","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/"},"author":{"name":"Noah Thompson","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a"},"headline":"How to send messages to the front end using WebSocket?","datePublished":"2024-03-16T02:48:50+00:00","dateModified":"2024-03-22T03:14:56+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/"},"wordCount":222,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/","url":"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/","name":"How to send messages to the front end using WebSocket? - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-16T02:48:50+00:00","dateModified":"2024-03-22T03:14:56+00:00","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-to-send-messages-to-the-front-end-using-websocket\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to send messages to the front end using WebSocket?"}]},{"@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\/2e83cc6ab9f60d36921c2d0f9f280f4a","name":"Noah Thompson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g","caption":"Noah Thompson"},"url":"https:\/\/www.silicloud.com\/blog\/author\/noahthompson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/24248","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\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=24248"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/24248\/revisions"}],"predecessor-version":[{"id":58270,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/24248\/revisions\/58270"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=24248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=24248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=24248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}