Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WSS example ? #20

Closed
extremety1989 opened this issue Jul 1, 2021 · 4 comments
Closed

WSS example ? #20

extremety1989 opened this issue Jul 1, 2021 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@extremety1989
Copy link

extremety1989 commented Jul 1, 2021

Hey,
can you please make example with secured websocket ?

something like in node/ws examples

const https = require('https');
const WebSocket = require('ws');

const server = https.createServer({
  cert: fs.readFileSync('/path/to/cert.pem'),
  key: fs.readFileSync('/path/to/key.pem')
});
const wss = new WebSocket.Server({ server });
wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });
  ws.send('something');
});
server.listen(8080);
@billywhizz
Copy link
Contributor

i'll have something soon that provides a (hopefully) nice and fast api for http, tls, websockets, templating, file serving, caching and middleware soon. it will be a separate module outside the "just" project on github but under "just-js" org for now. will let you know when i have something usable.

@billywhizz billywhizz added the enhancement New feature or request label Jul 9, 2021
@billywhizz billywhizz self-assigned this Jul 9, 2021
@tomByrer
Copy link

will let you know when i have something usable.

following!

@gauravAit
Copy link

gauravAit commented Dec 7, 2021

@billywhizz any update on this?

@billywhizz
Copy link
Contributor

hi @gauravAit. actually yes! apologies to all for lack of communication but i have not been able to focus a lot of time on this over last couple months. i have been hacking away on it though and should be able to pull the various pieces together over next few days and get a new release out.

i have a reasonably nice api now for http 1.1 servers and clients with tls and websocket support and from tests i have done performance on the techempower benchmarks should be pretty much the same even with a much nicer api. here's what the techempower code looks like now. much nicer than the pretty hideous PoC i put together for the benchmark last year!

const stringify = require('@stringify')
const html = require('@html')
const cache = require('@cache')
const dns = require('@dns')
const postgres = require('@pg')
const justify = require('@http')
const socket = require('@socket')
const util = require('lib/util.js')
const config = require('tfb.config.js')

const { getIPAddress } = dns
const { createSocket } = socket
const { createServer, responses } = justify
const { SimpleCache } = cache
const { sprayer, sortByMessage, getUpdateQuery } = util
const { sjs, attr } = stringify

const maxQuery = 500
const maxRows = 10000
const spray = sprayer(maxQuery)
const message = 'Hello, World!'
const json = { message }
const extra = { id: 0, message: 'Additional fortune added at request time.' }
const getRandom = () => Math.ceil(Math.random() * maxRows)
const getCount = (qs = { q: 1 }) => (Math.min(parseInt((qs.q) || 1, 10), maxQuery) || 1)
const sJSON = sjs({ message: attr('string') })
const wJSON = sjs({ id: attr('number'), randomnumber: attr('number') })

async function main () {
  const { db, fortunes, worlds, templates } = config
  const sock = createSocket()
  const ip = await getIPAddress(db.hostname)
  await sock.connect(ip, db.port || 5432)
  const pg = await postgres.createSocket(sock, db)
  const getWorldById = await pg.compile(worlds)
  const getFortunes = await pg.compile(fortunes)
  const worldCache = new SimpleCache(id => getWorldById(id))
  const template = html.load(templates.fortunes, templates.settings)
  const getRandomWorld = () => getWorldById(getRandom())
  const getRandomWorldCached = () => worldCache.get(getRandom())

  const server = createServer()
    .get('/plaintext', res => res.text(message))
    .get('/json', res => res.utf8(sJSON(json), responses.json))
    .get('/db', async res => res.utf8(wJSON(await getRandomWorld()), responses.json))
    .get('/fortunes', async res => res.html(template.call(sortByMessage([extra, ...await getFortunes()]))))
    .get('/cached-world', async (res, req) => res.json(await Promise.all(spray(getCount(req.query), getRandomWorldCached))))
    .get('/query', async (res, req) => res.json(await Promise.all(spray(getCount(req.query), getRandomWorld))))
    .get('/update', async (res, req) => {
      const count = getCount(req.query)
      const worlds = await Promise.all(spray(count, getRandomWorld))
      const updateWorlds = await getUpdateQuery(count, pg)
      await updateWorlds(...worlds.map(w => {
        w.randomnumber = getRandom()
        return [w.id, w.randomnumber]
      }).flat())
      res.json(worlds)
    })
    .listen('0.0.0.0', 8080)
}

main().catch(err => just.error(err.stack))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants