barechat-core
v3.0.0
Published
Core chat functionality for BareChat - reusable P2P networking components
Maintainers
Readme
BareChat Core
Core chat functionality for BareChat - reusable P2P networking components.
Installation
npm install barechat-coreTesting
Run the tests using brittle:
npm testUsage
import { getBackend } from 'barechat-core'
// Initialize the chat backend
const backend = getBackend({ bootstrap: ['192.168.0.123:55688'] })
// Join or create a chat room
const room = await backend.joinRoom('my-chat-room')
// Send a message
backend.sendMessage('Hello everyone!')
// Listen for incoming messages
backend.swarm.on('connection', (peer) => {
const memberId = backend.getMemberId(peer)
console.log(`New peer ${memberId} joined`)
peer.on('data', (rawData) => {
const event = JSON.parse(rawData)
console.log(`Message from ${memberId}:`, event.message)
})
})API
getBackend(opts)
Initializes the networking layer and returns an object containing the core API functions.
Parameters:
opts(Object, optional): Configuration options for the Hyperswarm instancebootstrap(string[], optional): A list of bootstrap servers to use for discovering peers
Returns:
createRoom: Function to create a new chat roomgetMemberId: Function to generate a short identifier for a peerjoinRoom: Function to join an existing chat room using a topic stringsendMessage: Function to send a message to all connected peersstrToTopic: Function to convert a string to a valid topic hashisHashcode: Function to validate if a string is a valid 64-character hex hashswarm: The underlying Hyperswarm instanceversion: The version of the BareChat package
createMessage(msg, local, messageType)
Creates a message object with timestamp and metadata.
Parameters:
msg(string): The content of the messagelocal(boolean, optional): Indicates whether the message is sent from a local devicemessageType(string, optional): The type of the message (defaults to 'text')
Using BareChat Core as a Package
To create variant chat experiences, you can import barechat-core in your project:
import { getBackend } from 'barechat-core'
const {
swarm,
getMemberId,
createRoom,
joinRoom,
sendMessage
} = getBackend()Example Usage
import { getBackend } from 'barechat-core'
// Initialize the chat backend
const backend = getBackend({ bootstrap: ['192.168.0.123:55688'] })
// Join or create a chat room
const room = await backend.joinRoom('my-chat-room')
// Send a message
backend.sendMessage('Hello everyone!')
// Listen for incoming messages
backend.swarm.on('connection', (peer) => {
const memberId = backend.getMemberId(peer)
console.log(`New peer ${memberId} joined`)
peer.on('data', (rawData) => {
const event = JSON.parse(rawData)
console.log(`Message from ${memberId}:`, event.message)
})
})For detailed API documentation, see the API documentation.
License
MIT
