{"id":64,"date":"2011-03-25T07:19:34","date_gmt":"2011-03-25T07:19:34","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2011\/03\/writing_a_udp_server_using_node_js-shtml\/"},"modified":"2011-03-25T07:19:34","modified_gmt":"2011-03-25T07:19:34","slug":"writing_a_udp_server_using_node_js-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_udp_server_using_node_js-shtml\/","title":{"rendered":"Writing A UDP Server Using Node.js"},"content":{"rendered":"<p>\nPreviously, I&#8217;ve written about <a href=\"\/robblog\/archive\/2011\/3\/Using_Node_js_To_Send_A_Heartbeat_To_A_Python_Server.shtml\">using node.js to send a heartbeat to a python server<\/a> using a UDP datagram.\n<\/p>\n<p>\nOf course you may not want to run <a href=\"http:\/\/www.python.org\/\">Python<\/a> if you using <a href=\"http:\/\/nodejs.org\/\">node.js<\/a> so I thought I&#8217;d give you a quick run through on writing a <acronym title=\"User Datagram Protocol\">UDP<\/acronym> server using node.js.\n<\/p>\n<p>\nThe earlier heartbeat example sent a small message saying &#8220;PyHB&#8221; to a server listening on port 43278. This example will listen out for that message and show it on the console to prove it&#8217;s working.\n<\/p>\n<p>\nThe first thing we need to do is to <code>require(\"dgram\")<\/code> to allow us access to the dgram library.\n<\/p>\n<pre class=\"lang:js decode:true \" >var dgram = require(\"dgram\");\n<\/pre>\n<p>\nWe now need to create a datagram socket to listen on, as we&#8217;re going to listen out for an IP4 datagram we create this of type <code>udp4<\/code>.\n<\/p>\n<pre class=\"lang:js decode:true \" >var server = dgram.createSocket(\"udp4\");\n<\/pre>\n<p>\nNode.js lets us bind to events, and <var>dgram<\/var> gives us three, <var>message<\/var>, <var>listening<\/var> and <var>close<\/var>.\n<\/p>\n<p>\nTo listen for an event, we need to use the <var>listening<\/var> event. As with other JavaScript events, we can pass in a callback function to actually do something when the event is fired. In this case, we&#8217;ll just use display the incoming message on the console, along with some details of the server that sent it.\n<\/p>\n<pre class=\"lang:js decode:true \" >server.on(\"message\", function (msg, rinfo) {\n  console.log(\"server got: \" + msg + \" from \" + rinfo.address + \":\" + rinfo.port);\n});\n<\/pre>\n<p>\nIt&#8217;s also useful to know we&#8217;ve actually setup our socket correctly, so we can use the <var>listening<\/var> event to return a message to console that everything has been setup OK and is working.\n<\/p>\n<pre class=\"lang:js decode:true \" >server.on(\"listening\", function () {\n  var address = server.address();\n  console.log(\"server listening \" + address.address + \":\" + address.port);\n});\n<\/pre>\n<p>\nFinally we need to actually bind our socket to a port so it can start listening for messages. For this we have to use the <code>bind<\/code> method.\n<\/p>\n<pre class=\"lang:js decode:true \" >server.bind(43278);\n<\/pre>\n<p>\nPutting this all together we have the following code that can listen for UDP datagrams and show them on the console.\n<\/p>\n<pre class=\"lang:js decode:true \" >var dgram = require(\"dgram\");\nvar server = dgram.createSocket(\"udp4\");\nserver.on(\"message\", function (msg, rinfo) {\n  console.log(\"server got: \" + msg + \" from \" + rinfo.address + \":\" + rinfo.port);\n});\nserver.on(\"listening\", function () {\n  var address = server.address();\n  console.log(\"server listening \" + address.address + \":\" + address.port);\n});\nserver.bind(43278);\n<\/pre>\n<p>\nI hope this has been a useful quick introduction to writing a UDP server in node.js.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously, I&#8217;ve written about using node.js to send a heartbeat to a python server using a UDP datagram. Of course you may not want to run Python if you using node.js so I thought I&#8217;d give you a quick run through on writing a UDP server using node.js. The earlier heartbeat example sent a small &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_udp_server_using_node_js-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Writing A UDP Server Using Node.js&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[31,44,69],"class_list":["post-64","post","type-post","status-publish","format-standard","hentry","category-dev","tag-javascript","tag-node-js","tag-udp"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Writing A UDP Server Using Node.js - Robert Price<\/title>\n<meta name=\"description\" content=\"An example of how to write a UDP server using node.js\" \/>\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.robertprice.co.uk\/robblog\/writing_a_udp_server_using_node_js-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Writing A UDP Server Using Node.js - Robert Price\" \/>\n<meta property=\"og:description\" content=\"An example of how to write a UDP server using node.js\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_udp_server_using_node_js-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2011-03-25T07:19:34+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/writing_a_udp_server_using_node_js-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/writing_a_udp_server_using_node_js-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Writing A UDP Server Using Node.js\",\"datePublished\":\"2011-03-25T07:19:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/writing_a_udp_server_using_node_js-shtml\\\/\"},\"wordCount\":311,\"keywords\":[\"JavaScript\",\"node.js\",\"UDP\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/writing_a_udp_server_using_node_js-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/writing_a_udp_server_using_node_js-shtml\\\/\",\"name\":\"Writing A UDP Server Using Node.js - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2011-03-25T07:19:34+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"description\":\"An example of how to write a UDP server using node.js\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/writing_a_udp_server_using_node_js-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/writing_a_udp_server_using_node_js-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/writing_a_udp_server_using_node_js-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Writing A UDP Server Using Node.js\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Writing A UDP Server Using Node.js - Robert Price","description":"An example of how to write a UDP server using node.js","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.robertprice.co.uk\/robblog\/writing_a_udp_server_using_node_js-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Writing A UDP Server Using Node.js - Robert Price","og_description":"An example of how to write a UDP server using node.js","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_udp_server_using_node_js-shtml\/","og_site_name":"Robert Price","article_published_time":"2011-03-25T07:19:34+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_udp_server_using_node_js-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_udp_server_using_node_js-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Writing A UDP Server Using Node.js","datePublished":"2011-03-25T07:19:34+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_udp_server_using_node_js-shtml\/"},"wordCount":311,"keywords":["JavaScript","node.js","UDP"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_udp_server_using_node_js-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_udp_server_using_node_js-shtml\/","name":"Writing A UDP Server Using Node.js - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2011-03-25T07:19:34+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"description":"An example of how to write a UDP server using node.js","breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_udp_server_using_node_js-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/writing_a_udp_server_using_node_js-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_udp_server_using_node_js-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Writing A UDP Server Using Node.js"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=64"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/64\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}