Skip to content

Instantly share code, notes, and snippets.

View codebanesr's full-sized avatar
🌿
Make a small contribution to the world

shanurrahman codebanesr

🌿
Make a small contribution to the world
View GitHub Profile

Sending images/Files/Binary Stream over websockets/socketio

Connect to the socket

this.socket = io(environment.socketUrl, {
  "transports": ["polling", "websocket"]
});

Start listening for the file upload event

watchForChange() {
  this.validateForm.valueChanges.subscribe(change=>{
    CobrowsingformComponent.mysequenceNumber = CobrowsingformComponent.mysequenceNumber+10;
    const msg = {
      nextsequenceNumber: CobrowsingformComponent.mysequenceNumber,
      uniqueId: CobrowsingformComponent.uniqueId,
      change
    }
handleData(data: any) {
  if(data.uniqueId === CobrowsingformComponent.uniqueId) {
    console.log("same id returning", CobrowsingformComponent.uniqueId, data.uniqueId)
    return;
  }

  if(CobrowsingformComponent.prevSequenceNumberReceived >= data.nextsequenceNumber) {
    return;
 }
/** Initializing all forms and socket to handle changes to form, Our current focus is on this.watchForChange*/
ngOnInit(): void {
    this.userType = "";
    CobrowsingformComponent.uniqueId = this.cobrowsingService.getUniqueId();
    CobrowsingformComponent.mysequenceNumber = parseInt(this.cobrowsingService.getSequenceNumber());
    this.initLoginForm();
    this.initializeForm();
    this.watchForChange();
} 
private fb: FormBuilder;
initializeForm() {
    this.validateForm = this.fb.group({
      accountNumber: [null, [ Validators.required]],
      firstName: [null, [Validators.required]],
      middleName: [null],
 lastName: [null, [Validators.required]],
    socket: SocketIOClient.Socket;
    subscribeToSocket() {
        this.socket = io("http://13.232.53.10:3000", {
            "transports": ["polling", "websocket"]
        });
        this.socket.on("sync", (data) => {
            console.log("sync event fired, syncing numbers")
            if (data.uniqueId != CobrowsingformComponent.uniqueId) {
                CobrowsingformComponent.prevSequenceNumberReceived = data.nextSequenceNumber - 10;