Creating Socket Client Connections
So far in this chapter, weâve explored the server side of Node.js sockets. Here weâll write a client program in Node.js 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:
â | â'use strict'â; |
â | âconstâ net = require(â'net'â); |
â | âconstâ client = net.connect({port: 60300}); |
â | client.on(â'data'â, data => { |
â | âconstâ message = JSON.parse(data); |
â | âifâ (message.type === â'watching'â) { |
â | console.log(â`Now watching: â${message.file}â`â); |
â | } âelseâ âifâ (message.type ... |
Get Node.js 8 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.