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

publish without peer #29

Open
1 task done
ToBinio opened this issue Mar 16, 2024 · 2 comments · May be fixed by #35
Open
1 task done

publish without peer #29

ToBinio opened this issue Mar 16, 2024 · 2 comments · May be fixed by #35

Comments

@ToBinio
Copy link

ToBinio commented Mar 16, 2024

Describe the feature

As far as I am concerned it is only possible to interact with channels/topics via peers.

It would be very usefull to have a global ws Obj which makes it possible to publish messages without a peer.

example usecase:
Having a chat app where only reviewing works with ws but sending does not.
In this case the HTTP request handle would not have any ws peer but would still need to publish a message.

Additional information

  • Would you be willing to help implement this feature?
@pi0
Copy link
Member

pi0 commented Mar 16, 2024

Sounds reasonable. Thinking more but i guess we can do it by introducing new setup hook and give an interface to it to interact.

@ToBinio
Copy link
Author

ToBinio commented Mar 22, 2024

by introducing new setup hook and give an interface to it to interact.

did you mean adding a new hook that gets called on init and gives a publisher that can publish to all connections? The user would then need to store the publisher somewhere it can be accessed.

something like this?

export interface Hooks extends Record<string, HookFn<any[], any>> {
  /** Called on init - Publish to all connections */
  publisher: (publisher: WSPublisher) => MaybePromise<void>;  // <- new hook

  /** Upgrading */
  upgrade: (req: WSRequest) => MaybePromise<void | { headers?: HeadersInit }>; 

  /** A message is received */
  message: (peer: Peer, message: Message) => MaybePromise<void>;

  /** A socket is opened */
  open: (peer: Peer) => MaybePromise<void>;

  /** A socket is closed */
  close: (
    peer: Peer,
    details: { code?: number; reason?: string },
  ) => MaybePromise<void>;

  /** An error occurs */
  error: (peer: Peer, error: WSError) => MaybePromise<void>;
}

for node this should be simple since the defineWebSocketAdapter does have access to the WebSocketServer so creating a publisher should be easy

for bun on the other hand we would need access to the Bun.serve() return value.
The only way I could think of would be to add a callback to the return value of the defineWebSocketAdapter

like this:

export interface BunAdapter {
  websocket: WebSocketHandler<ContextData>;
  handleUpgrade(req: Request, server: Server): Promise<boolean>;
  setServer(server: Server): void; // <- new part
}

this would have to be called after the Bun.serve() and would call the publisher hock internally

maybe I also completely misunderstood your comment...

@CS-Birb CS-Birb linked a pull request Apr 17, 2024 that will close this issue
8 tasks
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

Successfully merging a pull request may close this issue.

2 participants