Loading…
Client-side BRC-103 mutual authentication wrapper for socket.io-client. Signs all outbound messages and verifies inbound messages using a wallet, enabling authenticated peer-to-peer WebSocket communication.
npm install @bsv/authsocket-clientimport { AuthSocketClient } from '@bsv/authsocket-client'
import { PrivateKey, ProtoWallet } from '@bsv/sdk'
const clientWallet = new ProtoWallet(
PrivateKey.fromHex('0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')
)
const socket = AuthSocketClient('http://localhost:3000', { wallet: clientWallet })
socket.on('connect', () => {
console.log('Connected. Socket ID:', socket.id)
socket.emit('chatMessage', { text: 'Hello from client!' })
})
socket.on('chatMessage', msg => {
console.log('Server says:', msg)
})
socket.on('disconnect', () => {
console.log('Disconnected')
}).on(), .emit(), .id, .connect(), .disconnect()import { AuthSocketClient } from '@bsv/authsocket-client'
import { PrivateKey, ProtoWallet } from '@bsv/sdk'
const wallet = new ProtoWallet(
PrivateKey.fromHex('0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')
)
const socket = AuthSocketClient('http://localhost:3000', { wallet })
socket.on('connect', () => {
console.log('Authenticated')
socket.emit('joinRoom', 'general')
})
socket.on('message', data => {
console.log('Received:', data)
})const socket = AuthSocketClient('http://localhost:3000', {
wallet: myWallet,
managerOptions: {
reconnection: true,
reconnectionDelay: 1000,
reconnectionDelayMax: 5000,
reconnectionAttempts: 5
}
})const socket = AuthSocketClient('http://localhost:3000', {
wallet,
maxPendingAuthMessages: 32,
onError: (error, context) => {
console.error(context.phase, context.eventName, error)
}
})Malformed server authentication traffic and application callback failures are
contained before they can become unhandled rejections. The error context does
not include remote payloads or wallet material, and an onError handler that
throws or rejects is also contained.
@bsv/authsocket)@bsv/auth-express-middleware@bsv/message-box-client insteadPeer and Transport abstractions from SDKsign() and verify() methods@bsv/authsocket or implement BRC-103 Peer yourselfAuthSocketClient for live message delivery