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

nice finally XD a wrapper #1

Open
gurachan opened this issue Dec 8, 2019 · 9 comments
Open

nice finally XD a wrapper #1

gurachan opened this issue Dec 8, 2019 · 9 comments

Comments

@gurachan
Copy link

gurachan commented Dec 8, 2019

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
Copy link
Owner

hans00 commented Dec 10, 2019

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

So, may will spent some time.

@hans00
Copy link
Owner

hans00 commented Dec 18, 2019

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
Copy link
Owner

hans00 commented Dec 18, 2019

I had done the benchmark of Socket.IO

@gurachan
Copy link
Author

gurachan commented Dec 19, 2019

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
Copy link
Owner

hans00 commented Dec 19, 2019

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
Copy link
Owner

hans00 commented Dec 19, 2019

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
Copy link
Owner

hans00 commented Dec 22, 2019

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
Copy link
Owner

hans00 commented Dec 22, 2019

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
Copy link
Author

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

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

No branches or pull requests

2 participants