{"id":65,"date":"2011-03-24T20:52:13","date_gmt":"2011-03-24T20:52:13","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2011\/03\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\/"},"modified":"2011-03-24T20:52:13","modified_gmt":"2011-03-24T20:52:13","slug":"using_node_js_to_send_a_heartbeat_to_a_python_server-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\/","title":{"rendered":"Using Node.js To Send A Heartbeat To A Python Server"},"content":{"rendered":"<p>\nI&#8217;ve been looking back at some old code and I found an old piece on <a href=\"\/robblog\/archive\/2007\/5\/Using_Perl_To_Send_Heartbeat_To_A_Python_Server.shtml\">using Perl to send a heartbeat to a Python server<\/a>.\n<\/p>\n<p>\nRecently I&#8217;ve been using the excellent <a href=\"http:\/\/nodejs.org\/\">node.js<\/a> to do some cool JavaScript on the server side instead of in a client on a web broswer. I thought it would be fun to covert this simple script over to JavaScript.\n<\/p>\n<p>\nThis example is based on recipe 13.11 in the <a href=\"http:\/\/www.amazon.co.uk\/gp\/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.co.uk%2FPython-Cookbook-Alex-Martelli%2Fdp%2F0596007973%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1180551808%26sr%3D8-1&#038;tag=robertprcouk-21&#038;linkCode=ur2&#038;camp=1634&#038;creative=6738\">Python Cookbook<\/a>.\n<\/p>\n<p>\nThe script needs to send a UDP datagram to a server listening on port 43278. In this example the server will be listening on localhost, 127.0.0.1.\n<\/p>\n<p>\nAs with the Perl script, we&#8217;ll set a few &#8220;constants&#8221; to let us change things easily if we want to. As JavaScript doesn&#8217;t have constants in this example we&#8217;ll just use variables. Firstly we nee to require &#8220;dgram&#8221; so we can use UDP datagrams. We&#8217;ll set the server ip address, the port to use, the time in seconds to send the heartbeat, a debug flag if we want to see a message on the console everytime we send a message, and the message itself &#8220;PyHB&#8221;.\n<\/p>\n<div class=\"code\"><code>var dgram = require('dgram');<br \/>\nvar message = new Buffer(\"PyHB\");<br \/>\nvar server_ip = '127.0.0.1';<br \/>\nvar server_port = 43278;<br \/>\nvar beat_period = 5;<br \/>\nvar debug = 1;<br \/>\n<\/code><\/div>\n<p>\nWe&#8217;ll need to send out our heartbeat every <var>beat_period<\/var> seconds. To do this we can use JavaScript&#8217;s <code>setInterval<\/code> function to execute code every X milliseconds. As we&#8217;ve defined our <var>beat_period<\/var> in seconds, we&#8217;ll need to mulitply this value by 1000 to get the time in milliseconds.\n<\/p>\n<div class=\"code\"><code>setInterval(function() {<br \/>\n\/\/ datagram code goes here.<br \/>\n}, beat_period * 1000);<br \/>\n<\/code><\/div>\n<p>\nThe code to send the datagram is really simple, 3 lines infact. We create a client socket, send the message then close the client socket.\n<\/p>\n<div class=\"code\"><code>var client = dgram.createSocket(\"udp4\");<br \/>\nclient.send(message, 0, message.length, server_port, server_ip);<br \/>\nclient.close();<br \/>\n<\/code><\/div>\n<p>\nPutting this together, along with a bit of debugging information, we get the following code.\n<\/p>\n<div class=\"code\"><code>var dgram = require('dgram');<br \/>\nvar message = new Buffer(\"PyHB\");<br \/>\nvar server_ip = '127.0.0.1';<br \/>\nvar server_port = 43278;<br \/>\nvar beat_period = 5;<br \/>\nvar debug = 1;<br \/>\nconsole.log(\"Sending heartbeat to IP \" + server_ip + \" , port \" + server_port);<br \/>\nconsole.log(\"press Ctrl-C to stop\");<br \/>\nsetInterval(function() {<br \/>\nvar client = dgram.createSocket(\"udp4\");<br \/>\nclient.send(message, 0, message.length, server_port, server_ip);<br \/>\nclient.close();<br \/>\nif (debug)<br \/>\nconsole.log(\"Time: \" + new Date());<br \/>\n}, beat_period * 1000);<br \/>\n<\/code><\/div>\n<p>\nI hope that was a useful introduction to sending messages using UDP in Node.js.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been looking back at some old code and I found an old piece on using Perl to send a heartbeat to a Python server. Recently I&#8217;ve been using the excellent node.js to do some cool JavaScript on the server side instead of in a client on a web broswer. I thought it would be &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using Node.js To Send A Heartbeat To A Python Server&#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":[17,31,53],"class_list":["post-65","post","type-post","status-publish","format-standard","hentry","category-dev","tag-dev","tag-javascript","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using Node.js To Send A Heartbeat To A Python Server - Robert Price<\/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.robertprice.co.uk\/robblog\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using Node.js To Send A Heartbeat To A Python Server - Robert Price\" \/>\n<meta property=\"og:description\" content=\"I&#8217;ve been looking back at some old code and I found an old piece on using Perl to send a heartbeat to a Python server. Recently I&#8217;ve been using the excellent node.js to do some cool JavaScript on the server side instead of in a client on a web broswer. I thought it would be &hellip; Continue reading &quot;Using Node.js To Send A Heartbeat To A Python Server&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2011-03-24T20:52:13+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\\\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Using Node.js To Send A Heartbeat To A Python Server\",\"datePublished\":\"2011-03-24T20:52:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\\\/\"},\"wordCount\":305,\"keywords\":[\"Dev\",\"JavaScript\",\"Python\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\\\/\",\"name\":\"Using Node.js To Send A Heartbeat To A Python Server - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2011-03-24T20:52:13+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using Node.js To Send A Heartbeat To A Python Server\"}]},{\"@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":"Using Node.js To Send A Heartbeat To A Python Server - Robert Price","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\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Using Node.js To Send A Heartbeat To A Python Server - Robert Price","og_description":"I&#8217;ve been looking back at some old code and I found an old piece on using Perl to send a heartbeat to a Python server. Recently I&#8217;ve been using the excellent node.js to do some cool JavaScript on the server side instead of in a client on a web broswer. I thought it would be &hellip; Continue reading \"Using Node.js To Send A Heartbeat To A Python Server\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\/","og_site_name":"Robert Price","article_published_time":"2011-03-24T20:52:13+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\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Using Node.js To Send A Heartbeat To A Python Server","datePublished":"2011-03-24T20:52:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\/"},"wordCount":305,"keywords":["Dev","JavaScript","Python"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\/","name":"Using Node.js To Send A Heartbeat To A Python Server - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2011-03-24T20:52:13+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_node_js_to_send_a_heartbeat_to_a_python_server-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Using Node.js To Send A Heartbeat To A Python Server"}]},{"@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\/65","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=65"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}