Skip to content

Commit

Permalink
better description of connection process in README
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhus committed Dec 29, 2019
1 parent 64e183a commit 6af0dbe
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ await pubSub.listen('OrderCreated');
await pubSub.listen('ArticleUpdated');
~~~

BTW, the most reliable way is to initiate listening on `'connect'` event:

~~~typescript
pubSub.on('connect', async () => {
await Promise.all([
'UserChanged',
'OrderCreated',
'ArticleUpdated',
].map(channel => pubSub.listen(channel)));
});
~~~

Now, whenever you need to close/reopen connection, or reconnect occurred for any
reason you'll be sure nothing is broken.

### Handling messages

All payloads on messages are treated as JSON, so when the handler catch a
Expand Down Expand Up @@ -221,14 +236,16 @@ function printChannels(pubSub: PgPubSub) {
connectionString: 'postgres://postgres@localhost:5432/postgres',
});

pubSub.on('listen', channel => console.info(`Listening ${channel}...`));
pubSub.on('connect', () => {
console.info('Database connected!');
await pubSub.listen(CHANNEL);
});
pubSub.on('error', console.error);
pubSub.on('connect', () => console.info('Database connected!'));
pubSub.on('end', () => console.warn('Connection closed!'));
pubSub.on('listen', channel => console.info(`Listening ${channel}...`));
pubSub.channels.on(CHANNEL, console.log);

await pubSub.connect();
await pubSub.listen(CHANNEL);

setInterval(async () => {
await pubSub.notify('TestChannel', {
Expand Down

0 comments on commit 6af0dbe

Please sign in to comment.