-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nats: working on implementing sockets/channels compat layer (not done)
- Loading branch information
1 parent
d920317
commit 4463105
Showing
8 changed files
with
89 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Implement a websocket as exposed in Primus over NATS. | ||
*/ | ||
|
||
import { EventEmitter } from "events"; | ||
|
||
export class Socket extends EventEmitter { | ||
private listen: string; | ||
private send: string; | ||
private nc; | ||
private jc; | ||
|
||
constructor({ | ||
listen, | ||
send, | ||
nc, | ||
jc, | ||
}: { | ||
// subject to listen on | ||
listen: string; | ||
// subject to write to | ||
send: string; | ||
// nats connection | ||
nc; | ||
// json codec | ||
jc; | ||
}) { | ||
super(); | ||
this.listen = listen; | ||
this.send = send; | ||
this.nc = nc; | ||
this.jc = jc; | ||
this.startListening(); | ||
} | ||
|
||
private startListening = async () => { | ||
const sub = this.nc.subscribe(this.listen); | ||
for await (const mesg of sub) { | ||
const { data } = this.jc.decode(mesg.data) ?? ({} as any); | ||
this.emit("data", data); | ||
} | ||
}; | ||
|
||
write(data) { | ||
this.nc.publish(this.send, this.jc.encode({ data })); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import generateVouchers from "@cocalc/util/vouchers"; | ||
|
||
// nice alphanumeric string that can be used as nats subject, and very | ||
// unlikely to randomly collide with another browser tab from this account. | ||
export function randomId() { | ||
return generateVouchers({ count: 1, length: 10 })[0]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters