Creating Socket Client Connections
So far in this chapter, weâve explored the server side of Node sockets. Here weâll write a client program in Node to receive JSON messages from our net-watcher-json-service program. Weâll start with a naive implementation, and then improve upon it through the rest of the chapter.
Open an editor and insert this:
networking/net-watcher-json-client.js | |
â | â"use strict"â; |
â | âconstâ |
â | net = require(â'net'â), |
â | client = net.connect({port: 5432}); |
â | client.on(â'data'â, âfunctionâ(data) { |
â | âletâ message = JSON.parse(data); |
â | âifâ (message.type === â'watching'â) { |
â | console.log(â"Now watching: "â + message.file); |
â | } âelseâ âifâ (message.type === ... |
Get Node.js the Right Way now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.