hans00 / fastWS

Simple Node.js server based on uWebSockets
Apache License 2.0
27 stars 1 forks source link

nice finally XD a wrapper #1

Open gurachan opened 4 years ago

gurachan commented 4 years ago

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.

hans00 commented 4 years ago

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

So, may will spent some time.

hans00 commented 4 years ago

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.

hans00 commented 4 years ago

I had done the benchmark of Socket.IO

gurachan commented 4 years ago

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

hans00 commented 4 years ago

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 })
hans00 commented 4 years ago

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)

hans00 commented 4 years ago

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)
  }
})
hans00 commented 4 years ago

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.

gurachan commented 4 years ago

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