What is @types/sockjs?
The @types/sockjs npm package provides TypeScript type definitions for SockJS, a JavaScript library that provides a WebSocket-like object. SockJS gives you a coherent, cross-browser, Javascript API which creates a low latency, full duplex, cross-domain communication channel between the browser and the web server, with WebSockets or without. This package does not contain the functionality of SockJS itself but includes the type definitions to enable better development experience in TypeScript environments.
What are @types/sockjs's main functionalities?
Type Definitions for Server Initialization
This code sample demonstrates how to initialize a SockJS server with TypeScript, using the type definitions provided by @types/sockjs. It sets up a simple echo service on the server.
import * as http from 'http';
import * as sockjs from 'sockjs';
const echo = sockjs.createServer({ sockjs_url: 'http://cdn.jsdelivr.net/sockjs/1.0.1/sockjs.min.js' });
econst server = http.createServer();
echo.installHandlers(server, {prefix:'/echo'});
server.listen(9999, '0.0.0.0');
Type Definitions for Client Connection
This code sample shows how to connect to a SockJS server from the client side using TypeScript. It demonstrates handling the open, message, and close events of the SockJS connection.
import * as SockJS from 'sockjs-client';
let sock = new SockJS('http://mydomain.com/my_prefix');
sock.onopen = function() {
console.log('open');
sock.send('test');
};
sock.onmessage = function(e) {
console.log('message', e.data);
};
sock.onclose = function() {
console.log('close');
};
Other packages similar to @types/sockjs
@types/ws
Provides TypeScript definitions for the 'ws' package, a WebSocket client and server implementation. Unlike @types/sockjs, which is designed to work across different environments including those that do not support WebSockets, @types/ws is specifically for environments where WebSocket support is available.
@types/socket.io
Offers TypeScript definitions for Socket.IO, a real-time bidirectional event-based communication library. While @types/sockjs focuses on providing a consistent API across different browser and server implementations, @types/socket.io is more about enabling real-time capabilities in applications with a focus on event handling.
Installation
npm install --save @types/sockjs
Summary
This package contains type definitions for sockjs (https://github.com/sockjs/sockjs-node).
Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/sockjs.
import http = require("http");
export interface ServerOptions {
sockjs_url?: string | undefined;
prefix?: string | undefined;
response_limit?: number | undefined;
websocket?: boolean | undefined;
jsessionid?: any;
log?(severity: string, message: string): void;
heartbeat_delay?: number | undefined;
disconnect_delay?: number | undefined;
}
export function createServer(options?: ServerOptions): Server;
export interface Server extends NodeJS.EventEmitter {
installHandlers(server: http.Server, options?: ServerOptions): any;
on(event: "connection", listener: (conn: Connection) => any): this;
on(event: string, listener: Function): this;
}
export interface Connection extends NodeJS.ReadWriteStream {
remoteAddress: string;
remotePort: number;
address: {
[key: string]: {
address: string;
port: number;
};
};
headers: {
[key: string]: string;
};
url: string;
pathname: string;
prefix: string;
protocol: string;
readyState: number;
id: string;
close(code?: string, reason?: string): boolean;
destroy(): void;
on(event: "data", listener: (message: string) => any): this;
on(event: "close", listener: () => void): this;
on(event: string, listener: Function): this;
}
Additional Details
- Last updated: Tue, 07 Nov 2023 15:11:36 GMT
- Dependencies: @types/node
Credits
These definitions were written by Phil McCloghry-Laing.