npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chat-whatsapp

v1.3.0

Published

Made with create-react-library

Downloads

11

Readme

chat-whatsapp

Made with create-react-library

NPM JavaScript Style Guide

Install

npm install --save chat-whatsapp

Usage

Componente de Exemplo

import React, { useState, useEffect } from 'react';
import Chat from 'chat-whatsapp'; 
import io from 'socket.io-client'

const App = () => {
  const apiUrl = 'http://localhost:3001';
  const [isOpen, setIsOpen] = useState(false);
  const [messages, setMessages] = useState([]);
  const [inputValue, setInputValue] = useState('');
  const socket = io(apiUrl)

  useEffect(()=>{
    socket.on('response', (data) => {
      setMessages((prevMessages) => [...prevMessages, data])
    })

    return () => {
      socket.off('response')
    }
  }, [])

  return (
    <div> 
      <Chat 
        textChat="Olá, seja bem vindo de volta"  
        user="cer" 
        isOpen={isOpen} 
        setIsOpen={setIsOpen} 
        messages={messages}
        setMessages={setMessages}
        inputValue={inputValue}
        setInputValue={setInputValue}
        socket={socket}
      />
    </div> 
  );
};

export default App;

Exemplo do BackEnd

const express = require('express')
const http = require('http')
const socketIo = require('socket.io')

const app = express()
const server = http.createServer(app)
const io = socketIo(server, {
  cors: {
    origin: "*",
    methods: ["GET", "POST"]
  }
})

app.use((req, res, next) => {
  req.io = io;
  next();
});

io.on('connection', (socket) => {
  console.log('New client connected')
  
  socket.on('disconnect', () => {
    console.log('Client disconnected')
  })

  socket.on('send-message', (data) => {
    console.log('Message received: ', data)

    io.emit('response', { sender: 'bot', text: 'retorno do backend'});

  })
})

app.get('/send-message', (req, res) => {
  res.send('Backend is working');
});

server.listen(3001, () => {
  console.log('Server is running on port 3001')
})

Detalhe das variáveis

textChat: Mensagem de texto inicial exibida no chat.

Tipo: string
Exemplo: Olá, seja bem vindo de volta
user: Identificador do usuário.

Tipo: string
Exemplo: http://localhost:3001
apiURL: URL da API para conexão via WebSocket.

Tipo: boolean
Exemplo: true or false
isOpen: Estado que controla se o chat está aberto ou fechado.

Tipo: boolean
Exemplo: false
setIsOpen: Função para atualizar o estado de isOpen.

Tipo: function
Exemplo: setIsOpen(false)
messages: Array de mensagens exibidas no chat.

Tipo: array
Exemplo: [{ user: 'cer', message: 'Olá!' }]
setMessages: Função para atualizar o estado de messages.

Tipo: function
Exemplo: setMessages([{ user: 'cer', message: 'Olá!' }])
inputValue: Valor atual do campo de entrada do chat.

Tipo: string
Exemplo: ''
setInputValue: Função para atualizar o estado de inputValue.

Tipo: function
Exemplo: setInputValue('Olá!')
socket: Instância do WebSocket para comunicação em tempo real.

Tipo: object
Exemplo: io('http://localhost:3001')

License

MIT © IGBxRobinho