npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-ng-websocket

v0.3.1

Published

Simple WebSocket client for Angular

Downloads

8

Readme

simple-ng-websocket

NPM Test MIT License

This is simple WebSocket client for Angular. simple-ng-websocket is thin wrapper at Browser's WebSocket.

Install

To install simple-ng-websocket in the current directory, run:

npm install simple-ng-websocket

Usage

simple-ng-websocket can be use to Angular provider.

First, you must define it in the providers.

import { NgModule } from '@angular/core';
import { SimpleNgWebSocket, CONNECT_URL, LOGGER } from 'simple-ng-websocket';

@NgModule({
	providers: [
		{ provide: CONNECT_URL, useValue: 'ws://' + window.location.host + '/ws/' },
		{ provide: LOGGER, useValue: (level, message) => console.log(message) },
		SimpleNgWebSocket,
	],
})
export class AppModule { }

You can inject CONNECT_URL and LOGGER parameters.

  • CONNECT_URL : The URL to which to connect. If CONNECT_URL is not specified, the client connect to 'ws://' + window.location.host + '/' or 'wss://' + window.location.host + '/'.
  • LOGGER : The logger for this client's log event such as OPEN, CLOSE, SEND, RECEIVE and ERROR.

Then, you can use it on your logic code.

import { Injectable } from '@angular/core';
import { SimpleNgWebSocket } from 'simple-ng-websocket';

@Injectable()
export class SampleService {
	constructor(private ngws: SimpleNgWebSocket) {
		this.ngws.on('message', (msg) => {
			console.log(msg);
		});
	}

	sendMessage(msg: string): void {
		this.ngws.send(msg);
	}
}

API

Class: SimpleNgWebSocket

new SimpleNgWebSocket([url, logger])

  • url {String} It is the same as CONNECT_URL.
  • logger {Function} It is the same as LOGGER.

Create a new websocket connection. The constructor call connect() automatically.

Event: 'open'

  • ev {Event}
  • ngws {SimpleNgWebSocket}

Emitted when the handshake is complete. ev is through from Browser's WebSocket.

Event: 'message'

  • ev {MessageEvent}
  • ngws {SimpleNgWebSocket}

Emitted when a message is received from the server. ev is through from Browser's WebSocket.

Event: 'close'

  • ev {CloseEvent}
  • ngws {SimpleNgWebSocket}

Emitted when the connection is closed. ev is through from Browser's WebSocket.

Event: 'error'

  • ev {ErrorEvent}
  • ngws {SimpleNgWebSocket}

Emitted when an error occurs. ev is through from Browser's WebSocket.

ngws.connect()

Open a new websocket connection.

ngws.close()

  • code {Number} A numeric value indicating the status code explaining why the connection is being closed.
  • reason {String} A human-readable string explaining why the connection is closing.

Close the websocket connection.

ngws.send(message[, toJson])

  • message {String} The data to send to the server.
  • toJson {Boolean} Specifies whether message should be JSON.stringify() or not. Defaults to true.

Sends message through the connection. If the connection was not opend, send() call connect() automatically.

ngws.url

  • {String}

The URL of the WebSocket server.

ngws.ws

  • {WebSocket}

The connection instance of the raw WebSocket.

Example

You can find a example web application here.

License

MIT