{"__v":7,"_id":"556b7dbe9d4bb723007caa48","category":{"__v":2,"_id":"555fbf1a336c041700a9b87f","pages":["555fc09928249c1900618a91","556b7dbe9d4bb723007caa48"],"project":"555f9bf814d68e0d00966587","version":"555f9bf814d68e0d0096658a","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-05-22T23:43:22.085Z","from_sync":false,"order":4,"slug":"connectors","title":"Connectors"},"parentDoc":null,"project":"555f9bf814d68e0d00966587","user":"555f9a010758330d000d531a","version":{"__v":9,"_id":"555f9bf814d68e0d0096658a","project":"555f9bf814d68e0d00966587","createdAt":"2015-05-22T21:13:28.470Z","releaseDate":"2015-05-22T21:13:28.470Z","categories":["555f9bf814d68e0d0096658b","555f9e9ed7777119003acfee","555fa040d7777119003acffd","555fa344d7777119003ad00b","555fa3e6d7777119003ad00f","555fb694336c041700a9b86e","555fb779336c041700a9b86f","555fb86bd7777119003ad039","555fbf1a336c041700a9b87f"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-05-31T21:31:42.727Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":1,"body":"TCP connections to Pushjet work almost the same as [WebSocket](doc:websocket) connections. The mayor difference is how you receive data from the server. Each message starts with a `\\x02` and ends with a `\\x03`. The server authentication and how messages should be interpreted is the same. Authentication is done by sending the 36 byte UUID to the server. The default port for TCP connections is `7171`. Anything sent after the authentication is ignored by the server.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"#!/usr/bin/env python3\\nfrom uuid import uuid4\\nfrom socket import socket, AF_INET, SOCK_STREAM\\nfrom json import loads as json_decode\\n\\n\\nuuid   = uuid4()\\nserver = 'api.pushjet.io'\\nport   = 7171\\n\\n_MAGIC_START = b'\\\\002'\\n_MAGIC_END   = b'\\\\003'\\n\\nprint(\\\"Connecting as {} to {}:{}\\\".format(uuid, server, port))\\npj_sock = socket(AF_INET, SOCK_STREAM)\\npj_sock.connect((server, port))\\npj_sock.send(bytes(uuid, 'ASCII'))\\n\\ndef lazy_receiver(sock):\\n\\twhile True:\\n\\t\\tassert sock is not None\\n\\t\\tc, s = b'', b''\\n\\t\\twhile c != _MAGIC_START:\\n\\t\\t\\tc = sock.recv(1)\\n\\n\\t\\twhile c != _MAGIC_END:\\n\\t\\t\\tc = sock.recv(1)\\n\\t\\t\\tif c == '':\\n\\t\\t\\t\\tbreak\\n\\t\\t\\ts += c\\n\\n\\t\\tmsg = s[:-1].decode('UTF-8')\\n\\t\\t# Test if it's acknowledges the authentication\\n\\t\\tif msg == '{\\\"status\\\": \\\"ok\\\"}':\\n\\t\\t\\tcontinue\\n\\t\\tyield json_decode(msg)\\n    \\n# Listen for messages\\nfor message in lazy_receiver(pj_sock):\\n\\tif 'message' in message:\\n\\t\\thandle_message(message)\\n\\telif 'subscription' in message:\\n    handle_subscription(message)\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"tcp","type":"basic","title":"TCP"}
TCP connections to Pushjet work almost the same as [WebSocket](doc:websocket) connections. The mayor difference is how you receive data from the server. Each message starts with a `\x02` and ends with a `\x03`. The server authentication and how messages should be interpreted is the same. Authentication is done by sending the 36 byte UUID to the server. The default port for TCP connections is `7171`. Anything sent after the authentication is ignored by the server. [block:code] { "codes": [ { "code": "#!/usr/bin/env python3\nfrom uuid import uuid4\nfrom socket import socket, AF_INET, SOCK_STREAM\nfrom json import loads as json_decode\n\n\nuuid = uuid4()\nserver = 'api.pushjet.io'\nport = 7171\n\n_MAGIC_START = b'\\002'\n_MAGIC_END = b'\\003'\n\nprint(\"Connecting as {} to {}:{}\".format(uuid, server, port))\npj_sock = socket(AF_INET, SOCK_STREAM)\npj_sock.connect((server, port))\npj_sock.send(bytes(uuid, 'ASCII'))\n\ndef lazy_receiver(sock):\n\twhile True:\n\t\tassert sock is not None\n\t\tc, s = b'', b''\n\t\twhile c != _MAGIC_START:\n\t\t\tc = sock.recv(1)\n\n\t\twhile c != _MAGIC_END:\n\t\t\tc = sock.recv(1)\n\t\t\tif c == '':\n\t\t\t\tbreak\n\t\t\ts += c\n\n\t\tmsg = s[:-1].decode('UTF-8')\n\t\t# Test if it's acknowledges the authentication\n\t\tif msg == '{\"status\": \"ok\"}':\n\t\t\tcontinue\n\t\tyield json_decode(msg)\n \n# Listen for messages\nfor message in lazy_receiver(pj_sock):\n\tif 'message' in message:\n\t\thandle_message(message)\n\telif 'subscription' in message:\n handle_subscription(message)", "language": "python" } ] } [/block]