hans00 / fastWS

Simple Node.js server based on uWebSockets

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nice finally XD a wrapper

gurachan opened this issue · comments

can you benchmark this vs socket io, I really wanted to use this newly renamed uws xD but looking for a beautiful wrapper that I can fully understand.

commented

Ok, I will benchmark it.
But first we should implement Socket.IO protocol module.

So, may will spent some time.

commented

I had research Socket.IO.
Socket.IO combined HTTP XHR, long polling and WebSocket.
It's hard to fully support official package, so that maybe re-implement with official parser is easiest way.

In short-term may not implement it.

Still could benchmark Socket.IO, but may not fair because they have different workflow and WS protocol.

commented

I had done the benchmark of Socket.IO

don't worry I will wait as long xD it does a few socket io does. emit back and fort .. emit to specific id .. and had room feature. I will probably ditch socket io and create another new angular ngx-fastws-client

emit, once, on, connect, disconnect, binary

I still had to test this if this can connect faster than angular init xD like socket io and socket cluster does.

reason for it. to use this inside the angular guard.

/ClusterWS/cWS one failed on this test.

also, do you support custom parser? like allow to use other parser to parse binary and json. for encryption purposes.. parse -> emit

last setting origins to lock both express and socket to only listen to specific domain

commented

If only use room, emitter and binary, you can use basic protocol.
Because room feature is offical support by uWS.

app.ws('/path', (ws) => {
  ws.join('Room-ID')
  ws.on('binary', (binary) => {
    ws.sendBinary(Buffer or String)
    ws.broadcastBinary(Buffer or String)
  })
}, { protocol: 'basic' })

Or you can extents the basic protocol for custom parser.

const WSBasicProtocol = require('fast-ws/server/ws-protocol/basic')

class CustomProtcol extends WSBasicProtocol {
  incomingPacket (packet, isBinary) {
    // parse and emit
  }
 // I will add payload generator function at next release, that can just override it to change parser.
}

app.ws('/path', (ws) => {
}, { protocol: CustomProtcol })
commented

And origins lock, this code can do it.

app.ws('/path', (ws) => {
  if (/^https?:\/\/your.domain/.test(ws.requestHeaders['origin'])) {
    ws.close()
  }
})

I will consider to add it in ws options. (last argument in app.ws)

commented

The newest release may solve your problem.
It provide protocolOptions could assign your own parser.

Use with basic protocol.

app.ws('/path', ws => {
  ws.on('message', data => {
    ws.send(data)
  })
}, {
  protocolOptions: {
    parser: {
       stringify: data => something.encode(data),
       parse: data => something.decode(data)
    }
  }
})

Or use with fast-ws protocol. (It is alternative to Socket.io)

app.ws('/path', ws => {
  ws.on('event', ({ data, reply }) => {
    reply(data)
  })
  ws.on('message', data => {
    ws.send(data)
  })
}, {
  protocolOptions: {
     serialize: data => something.encode(data),
     deserialize: data => something.decode(data)
  }
})
commented

And about origin limit.
I'll not implement in options, because detect and close is not typical behavior.
Unless uWebSockets.js add support for origin limitation.

I see maybe soon, do you have an example of creating middleware for both ws and the express like