S
SolidJS4mo ago
sh1man

websocket get peers on the server

can you show an example of how to get websocket peers on the server solistart
15 Replies
sh1man
sh1man4mo ago
on the server I can't get peers
import { defineWebSocket, eventHandler } from "vinxi/http";

// Array of peers
export const peers: any = [];
export default eventHandler({
handler: () => {},
websocket: defineWebSocket({
async open(peer) {
// Add peer to the list of active
peers.push(peer);
console.log("WebSocket opened");
},

async message(peer, event) {
console.log("WebSocket message", event);
peer.send("YOOO");
},

async close(peer) {
console.log("WebSocket closed 3");
// Delete peer from the list of active
const index = peers.indexOf(peer);
if (index !== -1) {
peers.splice(index, 1);
}
},
}),
});
import { defineWebSocket, eventHandler } from "vinxi/http";

// Array of peers
export const peers: any = [];
export default eventHandler({
handler: () => {},
websocket: defineWebSocket({
async open(peer) {
// Add peer to the list of active
peers.push(peer);
console.log("WebSocket opened");
},

async message(peer, event) {
console.log("WebSocket message", event);
peer.send("YOOO");
},

async close(peer) {
console.log("WebSocket closed 3");
// Delete peer from the list of active
const index = peers.indexOf(peer);
if (index !== -1) {
peers.splice(index, 1);
}
},
}),
});
peerreynders
peerreynders4mo ago
FYI: The h3 documentation still states that websocket support requires the nightly build of h3. Vinxi only uses the standard v1.11.1 so full WS support may not be available yet. Just a thought. Try setting overrides/resolutions to npm:h3-nightly@latest and see if that has an effect (one way or another).
peerreynders
peerreynders4mo ago
It seems to be necessary to add
experimental: {
websocket: true,
},
experimental: {
websocket: true,
},
to your app.config.ts… if I'm reading this commit right.
peerreynders
peerreynders4mo ago
Note that the Nuxt example (using the nightly channel) uses a different approach.
GitHub
GitHub - pi0/nuxt-chat: A simple chat app made with Nuxt, Nitro Tas...
A simple chat app made with Nuxt, Nitro Tasks, Databases and Websockets! - pi0/nuxt-chat
peerreynders
peerreynders4mo ago
GitHub
GitHub - peerreynders/solid-start-ws-demo: Use a websocket server w...
Use a websocket server within SolidStart v1.0.0-rc.0 - peerreynders/solid-start-ws-demo
sh1man
sh1man3mo ago
Thanks, I'll try in your example from the solistart server I will not be able to send data to clients. Should we wait for the official release of ws with solidstart?
peerreynders
peerreynders3mo ago
If you look at the nuxt-chat example it's a matter of collecting the user/peers in a Map during the open event and removing them on the close event. Then the server can send a message to anyone stored in the map at any time. In the spirit of Plan to throw one away I'd use the opportunity to move ahead to figure out what you want to do and need. Once websocket support stabilizes it should be relatively quick to switch over. If you are only concerned with the server communicating with the client you could use SSE instead (which is part of the HTTP standard; websockets are not.) https://youtu.be/n9mRjkQg3VE
GitHub
nuxt-chat/server/api/chat-ws.ts at main · pi0/nuxt-chat
A simple chat app made with Nuxt, Nitro Tasks, Databases and Websockets! - pi0/nuxt-chat
Plan to Throw One Away
Notes for Northeastern University CS 5500
GitHub
nitro-sse-counter/src/server/event-stream.ts at main · peerreynders...
SSE POC with UnJS Nitro and a TS JSDoc frontend. Contribute to peerreynders/nitro-sse-counter development by creating an account on GitHub.
Fest Group
YouTube
SSE vs WebSockets vs Long Polling. Martin Chaov. JS Fest 2018
The talk from JS Fest conference in Kyiv, Ukraine. If you have a huge amount of data to deliver quickly you might have tried using web sockets to do so. However, sockets are hard to maintain and scale, not to mention multiplex. In this presentation I do a comparison of the three methods of delivering data to the front-end. Server-Sent Events gi...
sh1man
sh1man3mo ago
i can use sse to reset cache for all users using server ?
peerreynders
peerreynders3mo ago
I'm not understanding the question. Each client has a separate SSE response responsible for streaming any events from the server to the respective client but the server side code has access to centrally managed server resources/services while the response remains open. So the server can broadcast an event to all currently active event streams and each connected client can then take the necessary actions based on that event.
sh1man
sh1man3mo ago
I want to send information to all clients
sh1man
sh1man3mo ago
GitHub
GitHub - peerreynders/solid-start-sse-counter: Workaround demonstra...
Workaround demonstration for server side clean up after client closes EventSource - peerreynders/solid-start-sse-counter
sh1man
sh1man3mo ago
is this example relevant?
peerreynders
peerreynders3mo ago
This one would be more relevant https://github.com/peerreynders/solid-start-sse-chat but it hasn't been updated yet from the Beta 1 (0.2.26) days.
GitHub
GitHub - peerreynders/solid-start-sse-chat: Basic Chat demonstratio...
Basic Chat demonstration with server-sent events (SSE) - peerreynders/solid-start-sse-chat
sh1man
sh1man3mo ago
thank you, I'll see when I have time
peerreynders
peerreynders3mo ago
Now updated to SolidStart v1.0.0-rc.0 https://github.com/peerreynders/solid-start-sse-chat
GitHub
GitHub - peerreynders/solid-start-sse-chat: Basic Chat demonstratio...
Basic Chat demonstration with server-sent events (SSE) - peerreynders/solid-start-sse-chat