Introduction to Nssocket
Nssocket is an easy-to-use TCP socket library for Node.js which simplifies the creation of bidirectional RPC over TCP. This library offers a rich set of APIs to handle TCP connections, emit and listen to events, and transfer JSON-based messages effortlessly. Let’s dive into its features and see how we can utilize them with dozens of useful API examples!
Key APIs and Their Examples
1. Creating a Basic TCP Server
const nssocket = require('nssocket'); const server = nssocket.createServer((socket) => { socket.send('greeting', 'Hello, client!'); socket.data('greeting', (data) => { console.log('Received from client:', data); }); }); server.listen(6785); console.log('Server listening on port 6785');
2. Connecting a TCP Client
const nssocket = require('nssocket'); const client = new nssocket.NsSocket(); client.connect(6785, () => { console.log('Connected to server'); client.send('greeting', 'Hello, server!'); }); client.data('greeting', (data) => { console.log('Received from server:', data); });
3. Sending and Receiving JSON Data
const nssocket = require('nssocket'); const server = nssocket.createServer((socket) => { socket.send('jsonData', { message: 'Hello, client!' }); socket.data('jsonData', (data) => { console.log('Received JSON from client:', data); }); }); server.listen(6786); console.log('Server listening on port 6786'); const client = new nssocket.NsSocket(); client.connect(6786, () => { console.log('Connected to server'); client.send('jsonData', { message: 'Hello, server!' }); }); client.data('jsonData', (data) => { console.log('Received JSON from server:', data); });
4. Broadcasting Messages to All Clients
const nssocket = require('nssocket'); let clients = []; const server = nssocket.createServer((socket) => { clients.push(socket); socket.send('welcome', 'Welcome to the server!'); socket.data('broadcast', (data) => { clients.forEach(client => { client.send('broadcast', data); }); }); }); server.listen(6787); console.log('Server listening on port 6787'); const client1 = new nssocket.NsSocket(); const client2 = new nssocket.NsSocket(); client1.connect(6787); client2.connect(6787); client1.data('broadcast', (data) => { console.log('Client 1 received broadcast:', data); }); client2.data('broadcast', (data) => { console.log('Client 2 received broadcast:', data); }); setTimeout(() => { client1.send('broadcast', 'Hello from client 1!'); }, 3000);
Example Application: Simple Chat Server
Using the above APIs, let’s build a simple chat server where clients can communicate with each other in real-time.
// Server-side code const nssocket = require('nssocket'); const clients = {}; const server = nssocket.createServer((socket) => { socket.data('introduce', (username) => { clients[username] = socket; broadcast(`${username} joined the chat`); }); socket.data('message', (msg) => { broadcast(msg); }); socket.on('close', () => { const username = Object.keys(clients).find(key => clients[key] === socket); delete clients[username]; broadcast(`${username} left the chat`); }); }); function broadcast(msg) { Object.values(clients).forEach(client => { client.send('message', msg); }); } server.listen(6789); console.log('Chat server listening on port 6789');
// Client-side code const nssocket = require('nssocket'); const client = new nssocket.NsSocket(); client.connect(6789, () => { client.send('introduce', 'User_' + Math.random().toString(36).substr(2, 5)); }); client.data('message', (msg) => { console.log('New chat message:', msg); }); function sendMessage(msg) { client.send('message', msg); }
In this example, we create a chat server where each connected client is introduced using their username and sends/receives chat messages in real-time.
With such rich functionality, Nssocket truly makes TCP communications in Node.js efficient and straightforward!
Hash: a4cc29ba8ef5d1265196dde00d057a6ff8e66a3acf206e6004b7e863f21d4277