- Notifications
You must be signed in to change notification settings - Fork0
soketi/impl
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The 🇷🇺 Russian invasion of 🇺🇦 Ukraine breaches any law, including the UN Charter.#StandWithUkraine
Open-source is not about political views, but rather humanitar views. It's code by the people for the people. Unprovoked, unjustifiable and despicable action that is killing civilians is not tolerated. TheRenoki Co. subsidiaries (including Soketi) has taken action to move away from Russian software and dependencies and block any access from Russia within their projects.
Soketi Implementation is a TypeScript boilerplate to use on your WebSocket implementations. This represents a customizable single point of entry for your server, no matter what framework you are using.
The package comes with default implementations for the usual WebSocket operations, but as well as specific ones, like Pusher.
This is not providing a WebSocket server, but rather a way to implement your own WebSocket server, no matter what framework you are using.
In the examples, we will assume a pseudo-WebSocket server (not tied to any real use case).
This implementation provides a tracking of connections for the server:
import{Connections,Connection}from'@soketi/impl';constconns=newConnections();server.on('new-connection',asyncoriginalConnection=>{// Generate a unique ID for the connection.constuniqueId=Math.random()*1e5;// If possible, associate the unique ID with the original connection.// This can be used later to get the connection.originalConnection.id=uniqueId;// Create a new connection instance, binding// the send and close methods to the underlying WebSocket.constconnection=newConnection(uniqueId,{id:uniqueId,send:(message)=>originalConnection.send(message),close:(...args)=>originalConnection.close(...args),});// Add the connection to the connections tracker.awaitconns.newConnection(connection);});
This way, you can track connections and send messages to them:
for(constconnofconns.connections){// .send will call the send method of the underlying WebSocket.awaitconn.send('Hello!');}
To undo and remove a connection from the tracker, you can useremoveConnection
:
awaitconns.removeConnection(connection);
The package provides a way to handle WebSocket events at the general level,so that you don't have to implement them yourself. You will be defining boththe handlers, as well as the calls to them, in a static way.
import{RouterasWsRouter}from'@soketi/impl';import{Connections,Connection}from'@soketi/impl';constconns=newConnections();WsRouter.onNewConnection(asyncconn=>{// As explained earlier in the connections, you can use it to add it to a tracker.awaitconns.newConnection(conn);awaitconn.send('Hello!');});server.on('new-connection',asyncoriginalConnection=>{constconnection=newConnection(...);// Handle the connection via the router.// This will call the handler defined above.awaitWsRouter.handleNewConnection(connection);});
The router provides handlers for the following events:
onNewConnection(async (conn, ...args?) => {})
withhandleNewConnection(connection, ...args?)
onConnectionClosed(async (conn, code, msg, ...args?) => {})
withhandleConnectionClosed(connection, code, msg, ...args?)
onMessage(async (conn, message, ...args?) => {})
withhandleMessage(connection, message, ...args?)
onError(async (conn, error, ...args?) => {})
withhandleError(connection, error, ...args?)
You can also register your own handlers:
import{RouterasWsRouter}from'@soketi/impl';// Register a ping handler.WsRouter.registerHandler('onPing',asyncconn=>{awaitconn.send('Pong!');});server.on('ping',asyncoriginalConnection=>{// Get the existing connection on a ping or message.if(conns.connections.get(originalConnection.id)){awaitWsRouter.handle('onPing',connection);}});
- Public Channels ✅
- Presence Channels ✅
- Private Channels ✅
- Encrypted Private Channels ✅
- Client Events ✅
- Webhooks ❌
- REST API ❌
- Metrics ❌
See more:Pusher Channels