@addapptables/ng-web-socket
Version:
Web socket module for Angular
188 lines (154 loc) • 4.5 kB
Markdown
# ADDAPPTABLES ng-web-socket
ADDAPPTABLES ng-web-socket is a library for angular,
this library has adapters for socket-io and signalr
## Getting Started
For socket-io see the following [link](https://github.com/addapptables/ng-web-socket/tree/master/projects/addapptables/ng-socket-io)
For signal-r see the following [link](https://github.com/addapptables/ng-web-socket/tree/master/projects/addapptables/ng-signal-r)
Choose the version corresponding to your Angular version:
Angular | /ng-web-socket
----------- | -------------------
8 | 2.x
7 | 1.x
```
npm i /ng-web-socket --S
```
Create a websocket service
```typescript
// gateway with params
<SocketIOClient.ConnectOpts>({
url: 'http://localhost:8081',
autoConnect: true
})
export class WebSocketService {
// web socket connection
server: SocketIOClient.Socket;
// subscribe event
connectedUsers(data: any) {
console.log(data);
this.server.emit('my other event', { my: 'data' });
}
// emit events
sendMessage() {
this.server.emit('my other event', { my: 'data' });
}
}
```
import adapter and WebSocketService into the module
```typescript
import { NgWebSocketModule } from '@addapptables/ng-web-socket';
import { SocketIoAdapter } from '@addapptables/ng-socket-io';
export class AppModule { }
```
for child modules
```typescript
import { NgWebSocketModule } from '@addapptables/ng-web-socket';
export class AppModule { }
```
If you require a service to obtain the url, you can do the following
```typescript
// gateway with params
<SocketIOClient.ConnectOpts>({
autoConnect: false
})
export class WebSocketService implements ISocketWithOptions<SocketIOClient.ConnectOpts> {
// web socket connection
server: SocketIOClient.Socket;
constructor(
private _baseUrl: string,
private _managerSocketAdapter: ManagerSocketAdapter
) { }
//build options for websocket
withOptions(): WebSocketOptions<SocketIOClient.ConnectOpts> {
const token = '123';
const url = this._baseUrl + '?token=' + token;
return {
url
};
}
connect() {
this._managerSocketAdapter.connect(this);
}
// subscribe event
connectedUsers(data: any) {
console.log(data);
this.server.emit('my other event', { my: 'data' });
}
// emit events
sendMessage() {
this.server.emit('my other event', { my: 'data' });
}
}
```
Inject websocket into the component
```typescript
export class YourComponent implements OnInit {
constructor(
public _webSocketService: WebSocketService
) {
}
ngOnInit() {
this._webSocketService.connect();
}
}
```
## Custom adapters
Create a custom adapter:
```typescript
import * as io from 'socket.io-client';
import { ISocket } from '@addapptables/ng-web-socket';
// implements ISocket
export class SocketIoAdapter implements ISocket<SocketIOClient.Socket, SocketIOClient.ConnectOpts> {
// connect to ws transport
connect(url: string, options?: SocketIOClient.ConnectOpts): Promise<SocketIOClient.Socket> {
return new Promise((resolve, reject) => {
try {
const ioFunc = (io as any).default ? (io as any).default : io;
const connection = ioFunc(url, options);
resolve(connection.connect());
} catch (error) {
reject(error.message);
}
});
}
// bind events with current connection
bindEvent(connection: SocketIOClient.Socket, event: string, callFunction: (...args: any[]) => void) {
connection.on(event, callFunction);
}
// disconnect current connection
disconnect(connection: SocketIOClient.Socket): Promise<void> {
return new Promise((resolve, reject) => {
try {
connection.disconnect();
resolve();
} catch (error) {
reject(error.message);
}
});
}
}
```