Nico Jonsson

Nico Jonsson

1572421906

Using WebSockets with Angular and RxJS

WebSocket is a web communication protocol that allows two-way communication between a client and a server. What makes that technology appealing is that unlike regular TCP sockets, WebSockets use the same port as the HTTP protocol, which is port 80.

This means that when using WebSockets you don’t have to worry about firewalls blocking that port and preventing your data to flow between client and server. The technology has been around for a while, long enough to enjoy excellent support across all browsers according to caniuse.com:

This is image title

How to use WebSockets with RxJs?

This is actually almost too easy. You just have to import the webSocket function from RxJs, call it with the URL of your WebSocket, and… done!

import {webSocket, WebSocketSubject} from 'rxjs/webSocket';
myWebSocket: WebSocketSubject = webSocket('ws://localhost:8000');

You now have an RxJs subject (more info about subjects with this tutorial) that you can subscribe to in order to receive some data:

myWebSocket.asObservable().subscribe(dataFromServer => //...);

And if you want to send data to the server, simply use the next method of your subject:

myWebSocket.next({message: 'some message'});

Also note that when you subscribe to a WebSocket subject, you can register callbacks to be notified when an error happens or when the connection is closed, just like with a regular observable:

myWebSocket.subscribe(    
   msg => console.log('message received: ' + msg), 
   // Called whenever there is a message from the server    
   err => console.log(err), 
   // Called if WebSocket API signals some kind of error    
   () => console.log('complete') 
   // Called when connection is closed (for whatever reason)  
);

Thank you for reading !

If you enjoyed this post, please share it. Your help is always appreciated.

#angular #angular8 #typescript #rxjs #websocket

What is GEEK

Buddha Community

Using WebSockets with Angular and RxJS
Sasha  Roberts

Sasha Roberts

1626834660

Angular Rxjs: Use .map() And Async Pipe (Refactor Your Code)

In this video, we will see how to improve our code using the #rxs map operator and #angular #async pipe.

The components should be clean and minimal and should not have code that manipulates the data. Responsible for data manipulation is a service.
The goal is to prepare our data and return an #observable pipe so that we can use an #async pipe in the template.

code: https://github.com/profanis/codeShotsWithProfanis/tree/13/rxjsMapAndAsyncPipe

#angular #rxjs #observable #map #async

#angular rxjs #angular #angular tutorial #what is angular

Christa  Stehr

Christa Stehr

1598940617

Install Angular - Angular Environment Setup Process

Angular is a TypeScript based framework that works in synchronization with HTML, CSS, and JavaScript. To work with angular, domain knowledge of these 3 is required.

  1. Installing Node.js and npm
  2. Installing Angular CLI
  3. Creating workspace
  4. Deploying your First App

In this article, you will get to know about the Angular Environment setup process. After reading this article, you will be able to install, setup, create, and launch your own application in Angular. So let’s start!!!

Angular environment setup

Install Angular in Easy Steps

For Installing Angular on your Machine, there are 2 prerequisites:

  • Node.js
  • npm Package Manager
Node.js

First you need to have Node.js installed as Angular require current, active LTS or maintenance LTS version of Node.js

Download and Install Node.js version suitable for your machine’s operating system.

Npm Package Manager

Angular, Angular CLI and Angular applications are dependent on npm packages. By installing Node.js, you have automatically installed the npm Package manager which will be the base for installing angular in your system. To check the presence of npm client and Angular version check of npm client, run this command:

  1. npm -v

Installing Angular CLI

  • Open Terminal/Command Prompt
  • To install Angular CLI, run the below command:
  1. npm install -g @angular/cli

installing angular CLI

· After executing the command, Angular CLI will get installed within some time. You can check it using the following command

  1. ng --version

Workspace Creation

Now as your Angular CLI is installed, you need to create a workspace to work upon your application. Methods for it are:

  • Using CLI
  • Using Visual Studio Code
1. Using CLI

To create a workspace:

  • Navigate to the desired directory where you want to create your workspace using cd command in the Terminal/Command prompt
  • Then in the directory write this command on your terminal and provide the name of the app which you want to create. In my case I have mentioned DataFlair:
  1. Ng new YourAppName

create angular workspace

  • After running this command, it will prompt you to select from various options about the CSS and other functionalities.

angular CSS options

  • To leave everything to default, simply press the Enter or the Return key.

angular setup

#angular tutorials #angular cli install #angular environment setup #angular version check #download angular #install angular #install angular cli

Nico Jonsson

Nico Jonsson

1572421906

Using WebSockets with Angular and RxJS

WebSocket is a web communication protocol that allows two-way communication between a client and a server. What makes that technology appealing is that unlike regular TCP sockets, WebSockets use the same port as the HTTP protocol, which is port 80.

This means that when using WebSockets you don’t have to worry about firewalls blocking that port and preventing your data to flow between client and server. The technology has been around for a while, long enough to enjoy excellent support across all browsers according to caniuse.com:

This is image title

How to use WebSockets with RxJs?

This is actually almost too easy. You just have to import the webSocket function from RxJs, call it with the URL of your WebSocket, and… done!

import {webSocket, WebSocketSubject} from 'rxjs/webSocket';
myWebSocket: WebSocketSubject = webSocket('ws://localhost:8000');

You now have an RxJs subject (more info about subjects with this tutorial) that you can subscribe to in order to receive some data:

myWebSocket.asObservable().subscribe(dataFromServer => //...);

And if you want to send data to the server, simply use the next method of your subject:

myWebSocket.next({message: 'some message'});

Also note that when you subscribe to a WebSocket subject, you can register callbacks to be notified when an error happens or when the connection is closed, just like with a regular observable:

myWebSocket.subscribe(    
   msg => console.log('message received: ' + msg), 
   // Called whenever there is a message from the server    
   err => console.log(err), 
   // Called if WebSocket API signals some kind of error    
   () => console.log('complete') 
   // Called when connection is closed (for whatever reason)  
);

Thank you for reading !

If you enjoyed this post, please share it. Your help is always appreciated.

#angular #angular8 #typescript #rxjs #websocket

Cómo Usar El Operador SwitchMap En RxJS En angular

Tutorial sobre el uso del operador switchMap en #RxJS (ReactiveX) en #Angular. Dura más de lo habitual debido a que lo explicamos con dos ejemplos, uno muy básico y uno de los típicamente llamados "Ejemplo de la Vida Real" (Real life example). Para ver los puntos más importantes, les dejamos los siguientes minutos:

#rxjs #angular 

Ayyaz Zafar

1624138795

Angular Material Autocomplete - Multiple Use Cases covered

Learn How to use Angular Material Autocomplete Suggestions Search Input. I covered multiple use cases.

Please watch this video. I hope this video would be helpful for you to understand it and use it in your projects

Please subscribe: https://www.youtube.com/channel/UCL5nKCmpReJZZMe9_bYR89w

#angular #angular-material #angular-js #autocomplete #angular-material-autocomplete #angular-tutorial