Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
@tsed/socketio
Advanced tools
Experimental feature. You can contribute to improve this feature !
A package of Ts.ED framework. See website: https://romakita.github.io/ts-express-decorators/
Socket.io enable real-time bidirectional event-based communication. It works on every platform, browser or device, focusing equally on reliability and speed.
Before using the Socket.io, we need to install the Socket.io module.
npm install --save socket.io @types/socket.io @tsed/socketio
Then add the following configuration in your ServerLoader:
import {ServerLoader, ServerSettings} from "@tsed/common";
import "@tsed/socketio"; // import socketio Ts.ED module
import Path = require("path");
const rootDir = Path.resolve(__dirname)
@ServerSettings({
rootDir,
socketIO: {
// ... see configuration
}
})
export class Server extends ServerLoader {
}
Socket.IO allows you to “namespace” your sockets, which essentially means assigning different endpoints or paths. This is a useful feature to minimize the number of resources (TCP connections) and at the same time separate concerns within your application by introducing separation between communication channels. See namespace documentation.
All Socket service work under a namespace and you can create one Socket service per namespace.
Example:
import * as SocketIO from "socket.io";
import {SocketService, IO, Nsp, Socket, SocketSession} from "@tsed/socketio";
@SocketService("/my-namespace")
export class MySocketService {
@Nsp nsp: SocketIO.Namespace;
constructor(@IO private io: SocketIO.Server) {}
/**
* Triggered the namespace is created
*/
$onNamespaceInit(nsp: SocketIO.Namespace) {
}
/**
* Triggered when a new client connects to the Namespace.
*/
$onConnection(@Socket socket: SocketIO.Socket, @SocketSession session: SocketSession) {
}
/**
* Triggered when a client disconnects from the Namespace.
*/
$onDisconnect(@Socket socket: SocketIO.Socket) {
}
}
@SocketService inherit from @Service decorator. That means, a SocketService can be injected to another Service, Controller or Middleware.
@Input decorator declare a method as a new handler for a specific event
.
@SocketService("/my-namespace")
export class MySocketService {
@Input("eventName")
myMethod(@Args(0) userName: string, @Socket socket: SocketIO.Socket, @Nsp nsp: SocketIO.Namespace) {
console.log(userName);
}
}
You have a many choice to send a response to your client. Ts.ED offer some decorators to send a response:
Example:
@SocketService("/my-namespace")
export class MySocketService {
@Input("eventName")
@Emit("responseEventName") // or Broadcast or BroadcastOthers
async myMethod(@Args(0) userName: string, @Socket socket: SocketIO.Socket) {
return "Message " + userName;
}
}
The method accept a promise as returned value.
!> Return value is only possible when the method is decorated by @Emit, @Broadcast and @BroadcastOthers.
Ts.ED create a new session for each socket.
@SocketService("/my-namespace")
export class MySocketService {
@Input("eventName")
@Emit("responseEventName") // or Broadcast or BroadcastOthers
async myMethod(@Args(0) userName: string, @SocketSession session: SocketSession) {
const user = session.get("user") || {}
user.name = userName;
session.set("user", user);
return user;
}
}
See our documentation https://romakita.github.io/ts-express-decorators/#/api/index
FAQs
Socket.io package for Ts.ED framework
The npm package @tsed/socketio receives a total of 767 weekly downloads. As such, @tsed/socketio popularity was classified as not popular.
We found that @tsed/socketio demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.