node-hello-prayag
v1.0.8
Published
A demo project for publishing an npm package.
Downloads
15
Readme
node-hello-prayag
A set of handlers for one-to-one chat functionality using Socket.io.
Installation
Install the package using npm:
npm install node-hello-prayag
Usage
First, require the package in your server file:
const { initUser, sendMessageHandler, startTyping, stopTyping, handleDisconnect } = require('node-hello-prayag');
Here is a complete example of how to set up a Socket.io server with these handlers:
const http = require('http');
const express = require('express');
const socketIo = require('socket.io');
const { initUser, sendMessageHandler, startTyping, stopTyping, handleDisconnect } = require('node-hello-prayag');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
// List of active users
const activeUsers = [];
io.on('connection', (socket) => {
console.log('A user connected:', socket.id);
// Initialize user
socket.on('initUser', initUser(socket, io));
// Handle sending messages
socket.on('sendMessage', sendMessageHandler(socket, io));
// Handle typing start
socket.on('startTyping', startTyping(socket, io));
// Handle typing stop
socket.on('stopTyping', stopTyping(socket, io));
// Handle user disconnection
socket.on('disconnect', handleDisconnect(socket, io));
});
server.listen(3000, () => {
console.log('Server is listening on port 3000');
});