Skip to content

Commit

Permalink
feat(server): Use uWebSockets (enisdenjo#89)
Browse files Browse the repository at this point in the history
Closes enisdenjo#61

Co-authored-by: enisdenjo <badurinadenis@gmail.com>
  • Loading branch information
maxpain and enisdenjo committed Apr 11, 2021
1 parent 76092c5 commit 45d08fc
Show file tree
Hide file tree
Showing 14 changed files with 1,052 additions and 610 deletions.
20 changes: 20 additions & 0 deletions README.md
Expand Up @@ -1311,6 +1311,26 @@ async function ping() {

</details>

<details id="uws">
<summary><a href="#uws">🔗</a> Server usage with <a href="https://github.com/uNetworking/uWebSockets.js">uWebSockets.js</a></summary>

```ts
import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<tag>
import { makeBehavior } from 'graphql-ws/lib/use/uWebSockets';
import { schema } from './my-graphql-schema';

uWS
.App()
.ws('/graphql/is-performant', makeBehavior({ schema }))
.listen(80, (listenSocket) => {
if (listenSocket) {
console.log('Listening to port 80');
}
});
```

</details>

## [Documentation](docs/)

Check the [docs folder](docs/) out for [TypeDoc](https://typedoc.org) generated documentation.
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Expand Up @@ -11,4 +11,5 @@ graphql-ws
- [protocol](modules/protocol.md)
- [server](modules/server.md)
- [types](modules/types.md)
- [use/uWebSockets](modules/use_uwebsockets.md)
- [use/ws](modules/use_ws.md)
31 changes: 31 additions & 0 deletions docs/interfaces/use_uwebsockets.extra.md
@@ -0,0 +1,31 @@
[graphql-ws](../README.md) / [use/uWebSockets](../modules/use_uwebsockets.md) / Extra

# Interface: Extra

[use/uWebSockets](../modules/use_uwebsockets.md).Extra

The extra that will be put in the `Context`.

## Table of contents

### Properties

- [request](use_uwebsockets.extra.md#request)
- [socket](use_uwebsockets.extra.md#socket)

## Properties

### request

`Readonly` **request**: HttpRequest

The initial HTTP request before the actual
socket and connection is established.

___

### socket

`Readonly` **socket**: WebSocket

The actual socket connection between the server and the client.
32 changes: 32 additions & 0 deletions docs/modules/use_uwebsockets.md
@@ -0,0 +1,32 @@
[graphql-ws](../README.md) / use/uWebSockets

# Module: use/uWebSockets

## Table of contents

### Interfaces

- [Extra](../interfaces/use_uwebsockets.extra.md)

### Functions

- [makeBehavior](use_uwebsockets.md#makebehavior)

## Functions

### makeBehavior

**makeBehavior**(`options`: [*ServerOptions*](../interfaces/server.serveroptions.md)<[*Extra*](../interfaces/use_uwebsockets.extra.md)\>, `behavior?`: uWS.WebSocketBehavior, `keepAlive?`: *number*): uWS.WebSocketBehavior

Make the behaviour for using a [uWebSockets.js](https://github.com/uNetworking/uWebSockets.js) WebSocket server.
This is a basic starter, feel free to copy the code over and adjust it to your needs

#### Parameters:

Name | Type |
:------ | :------ |
`options` | [*ServerOptions*](../interfaces/server.serveroptions.md)<[*Extra*](../interfaces/use_uwebsockets.extra.md)\> |
`behavior` | uWS.WebSocketBehavior |
`keepAlive` | *number* |

**Returns:** uWS.WebSocketBehavior
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -98,6 +98,7 @@
"typedoc": "^0.20.34",
"typedoc-plugin-markdown": "^3.6.0",
"typescript": "^4.2.3",
"uWebSockets.js": "uNetworking/uWebSockets.js#v19.0.0",
"ws": "^7.4.4"
}
}
26 changes: 13 additions & 13 deletions src/tests/client.ts
Expand Up @@ -4,9 +4,9 @@

import WebSocket from 'ws';
import { EventEmitter } from 'events';
import { startTServer } from './fixtures/simple';
import { createClient, Client, EventListener } from '../client';
import { SubscribePayload } from '../message';
import { startWSTServer as startTServer } from './utils';

// simulate browser environment for easier client testing
beforeEach(() => {
Expand Down Expand Up @@ -291,7 +291,7 @@ it('should close the socket if the `connectionParams` rejects or throws', async
it('should not send the complete message if the socket is not open', async () => {
const {
url,
clients,
getClients,
waitForOperation,
waitForClientClose,
} = await startTServer();
Expand Down Expand Up @@ -319,7 +319,7 @@ it('should not send the complete message if the socket is not open', async () =>
await waitForOperation();

// kick the client off
for (const client of clients) {
for (const client of getClients()) {
client.close();
await waitForClientClose();
}
Expand Down Expand Up @@ -514,7 +514,7 @@ describe('subscription operation', () => {

await server.waitForClientClose();

expect(server.clients.size).toBe(0);
expect(server.getClients().length).toBe(0);
});

it('should dispose of the subscription on error', async () => {
Expand All @@ -534,13 +534,13 @@ describe('subscription operation', () => {

await server.waitForClientClose();

expect(server.clients.size).toBe(0);
expect(server.getClients().length).toBe(0);
});

it('should stop dispatching messages after completing a subscription', async () => {
const {
url,
clients,
getClients,
waitForOperation,
waitForComplete,
} = await startTServer();
Expand All @@ -553,8 +553,8 @@ describe('subscription operation', () => {
);
await waitForOperation();

for (const client of clients) {
client.once('message', () => {
for (const client of getClients()) {
client.onMessage(() => {
// no more messages from the client
fail("Shouldn't have dispatched a message");
});
Expand Down Expand Up @@ -597,7 +597,7 @@ describe('"concurrency"', () => {
}, 10);
await sub1.waitForComplete();

expect(server.clients.size).toBe(1);
expect(server.getClients().length).toBe(1);
});
});

Expand Down Expand Up @@ -1018,7 +1018,7 @@ describe('reconnecting', () => {
await server.waitForClientClose();

// and no clients should be left
expect(server.clients.size).toBe(0);
expect(server.getClients().length).toBe(0);
});

it('should lazy disconnect even if subscription is created during retries after all get completed', async () => {
Expand Down Expand Up @@ -1062,7 +1062,7 @@ describe('reconnecting', () => {
}, 10);

// and client should still be connected
expect(server.clients.size).toBe(1);
expect(server.getClients().length).toBe(1);

// dispose of the last subscription
sub2.dispose();
Expand All @@ -1072,7 +1072,7 @@ describe('reconnecting', () => {
await server.waitForClientClose();

// and all connections are gone
expect(server.clients.size).toBe(0);
expect(server.getClients().length).toBe(0);
});
});

Expand Down Expand Up @@ -1127,7 +1127,7 @@ describe('events', () => {

expect(closedFn).not.toBeCalled();

server.clients.forEach((client) => {
server.getClients().forEach((client) => {
client.close();
});

Expand Down

0 comments on commit 45d08fc

Please sign in to comment.