lipp/node-jet

View on GitHub
examples/chat/server/index.ts

Summary

Maintainability
A
0 mins
Test Coverage
#!/usr/bin/env node

import { Daemon, LogLevel, Method, Peer, State } from '../../../lib/jet.js'

const wsPort = parseInt(process.argv[2]) || 8081
const internalPort = 10222

// Create Jet Daemon
var daemon = new Daemon({
  features: {
    fetch: 'full',
    asNotification: false
  }
})
daemon.listen({
  tcpPort: internalPort,
  wsPort: wsPort
})
console.log('chat-server ready')
console.log('listening on port', wsPort)

// Create Jet Peer
const peer = new Peer({
  port: internalPort,
  log: { logName: '', logCallbacks: [console.log], logLevel: LogLevel.socket }
})

// the messages state is simply an array
// it cannot be "set" directly.
// instead use 'chat/append' and 'chat/clear'
// to modify it.
const messages = new State<string[]>('chat/messages', [])

const append = new Method('chat/append')
append.on('call', (args) => {
  // get last messages
  var msgs = messages.value()
  // append new one
  msgs.push(args.message)
  // publish change
  messages.value([...msgs])
})

const clear = new Method('chat/clear')
clear.on('call', () => {
  messages.value([])
})
peer
  .connect()
  .then(() => {
    peer.batch(() => {
      peer.add(messages)
      peer.add(append)
      peer.add(clear)
    })
  })
  .then(() => {})