# Socket.io Ionic

**URL:** <https://forum.ionicframework.com/t/socket-io-ionic/56280>\
**Category:** ionic-v3\
**Created:** [June 30, 2016, 10:18am UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280 "2016-06-30T10:18:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jose\_Ionic](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jose\_Ionic](https://forum.ionicframework.com/u/Jose_Ionic)\
**Post date:** [June 30, 2016, 10:18am UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/1 "2016-06-30T10:18:14Z")

</div>

Hello! Somebody know where I can find a simple example of socket with Ionic 2? I found examples with the extension (.js) but not (.ts) and I’m really lost.  
Thanks in advance.

Best regards.

---

<div class="post-metadata">

**Author:** ![bengtler](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bengtler/32/98565_2.png) [@bengtler](https://forum.ionicframework.com/u/bengtler)\
**Post date:** [June 30, 2016, 10:59am UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/2 "2016-06-30T10:59:26Z")

</div>

i think you can simply add [socket.io](http://socket.io) to your package.json and install the typings for socket io --\>

[https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/socket.io/socket.io.d.ts](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/socket.io/socket.io.d.ts)

install the typings module: `npm install -g typings`  
after that you can install the typings for [socket.io](http://socket.io) typings install

`typings install socket.io-client --save --ambient`

if “–ambient” does not work any more your can try “–global” instead

---

<div class="post-metadata">

**Author:** ![beetz12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/beetz12/32/100830_2.png) [@beetz12](https://forum.ionicframework.com/u/beetz12)\
**Post date:** [June 30, 2016, 2:22pm UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/3 "2016-06-30T14:22:25Z")

</div>

I found this tutorial on ionic2 and [socket.io](http://socket.io)  
[https://www.thepolyglotdeveloper.com/2016/01/creating-a-real-time-chat-application-with-ionic-2-and-socket-io/](https://www.thepolyglotdeveloper.com/2016/01/creating-a-real-time-chat-application-with-ionic-2-and-socket-io/)

And here is the project on github:

> **[couchbaselabs/ionic2-web-chat](https://github.com/couchbaselabs/ionic2-web-chat)**
>
> Contribute to ionic2-web-chat development by creating an account on GitHub.

Unfortunately the project files were a bit old and it didn’t suit my needs so I updated it to the latest Ionic2 + Angular2 and converted to typescript. You can check it out in my forked repo:

> **[beetz12/ionic2-web-chat](https://github.com/beetz12/ionic2-web-chat)**
>
> Contribute to ionic2-web-chat development by creating an account on GitHub.

Good luck, let me know if you have any question.

---

<div class="post-metadata">

**Author:** ![matheo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/matheo/32/91507_2.png) [@matheo](https://forum.ionicframework.com/u/matheo)\
**Post date:** [June 30, 2016, 2:29pm UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/4 "2016-06-30T14:29:54Z")

</div>

I’ve installed [Socket.io](http://Socket.io) client and typings:

```auto
npm install socket.io-client --save
typings install dt~socket.io-client --save --global

```

`ambient` is [deprecated](https://github.com/driftyco/ionic2-app-base/tree/master/typings) in favor of `globals`.

@Jose_Ionic Using the client in Ionic 2 is simple as:

```auto
import * as io from "socket.io-client";
...

export class YourClass {
  socket: SocketIOClient.Socket;

  ...

  connect() {
    this.socket = io(SOCKET_URL);
    this.socket.on('connect', () => {
      ...
    });
  }
}

```

Have fun!

---

<div class="post-metadata">

**Author:** ![Jose\_Ionic](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jose\_Ionic](https://forum.ionicframework.com/u/Jose_Ionic)\
**Post date:** [June 30, 2016, 3:51pm UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/5 "2016-06-30T15:51:29Z")

</div>

Thank you very very much for your responses 🙂

@beetz12 -\> Thank you for your contribution, I saw that tutorial a few days ago and I liked it, I try to pass it to .ts but I could not :S .

@matheo -\> Thank you for your contribution too, the command “npm install socket.io-client --save” has worked but not the same for the second “typings install socket.io-client --save --global”

- “Typings” is not recognized as an internal or external command. Do you know why?

With your information my .ts is like that:

import {Component} from “@angular/core”;  
import {NavController , Alert} from ‘ionic-angular’;  
import {Http} from ‘@angular/http’;  
import \* as io from “socket.io-client”;

@Component({  
templateUrl: ‘build/pages/home/home.html’  
})

export class HomePage {  
constructor(private navController: NavController , private socket: SocketIOClient.Socket) {  
this.navController=navController;  
this.socket=socket;  
}

```
connect() {
    this.socket = io(192.168.1.100:8080);
    this.socket.on('connect', function () {
        socket.emit('news', { hello: 'world' });
    });
}

```

}

I have an error: “TS1005: ‘,’ expected.” and the other “cannot find namespace ‘socketIoClient’”, I think that the last one is because the second command doen’t work.

I would like do one example that I press a button like this:

\<ion-navbar \*navbar\>  
  
My Proyect

Send Socket

and send socket to my server in 192.168.1.100.8080 so when I finalize and tested it, I will put it here, if you are interested.  
Thank you for your help.

Best regards.

---

<div class="post-metadata">

**Author:** ![matheo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/matheo/32/91507_2.png) [@matheo](https://forum.ionicframework.com/u/matheo)\
**Post date:** [June 30, 2016, 3:56pm UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/6 "2016-06-30T15:56:16Z")

</div>

Woot, sorry, my copy+paste error, it needs the DT:

```auto
typings install dt~socket.io-client --save --global

```

And to initialize the socket, you need the URL as string, with protocol too:

```auto
this.socket = io('http://192.168.1.100:8080');

```

---

<div class="post-metadata">

**Author:** ![Jose\_Ionic](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jose\_Ionic](https://forum.ionicframework.com/u/Jose_Ionic)\
**Post date:** [June 30, 2016, 4:07pm UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/7 "2016-06-30T16:07:21Z")

</div>

Yes, thank you! Now I dont have my error “TS1005: ‘,’ expected.” but with the new command I have the same error: “Typings” is not recognized as an internal or external command. So now, when I run my proyect I have the error “Cannot find module ‘socket.io-client’”

---

<div class="post-metadata">

**Author:** ![beetz12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/beetz12/32/100830_2.png) [@beetz12](https://forum.ionicframework.com/u/beetz12)\
**Post date:** [June 30, 2016, 4:07pm UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/8 "2016-06-30T16:07:22Z")

</div>

Did you check out my fork?

> **[couchbaselabs/ionic2-web-chat](https://github.com/couchbaselabs/ionic2-web-chat)**
>
> Contribute to ionic2-web-chat development by creating an account on GitHub.

I converted the project to typescript and also upgraded to Angular RC1 latest ionic2

---

<div class="post-metadata">

**Author:** ![beetz12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/beetz12/32/100830_2.png) [@beetz12](https://forum.ionicframework.com/u/beetz12)\
**Post date:** [June 30, 2016, 4:08pm UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/9 "2016-06-30T16:08:27Z")

</div>

You are missing the typings npm module. You can install it by:

`$ npm install typings -g`

---

<div class="post-metadata">

**Author:** ![Jose\_Ionic](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jose\_Ionic](https://forum.ionicframework.com/u/Jose_Ionic)\
**Post date:** [June 30, 2016, 4:20pm UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/10 "2016-06-30T16:20:29Z")

</div>

Yes, but your file “home.js” is not .ts I will need .ts

---

<div class="post-metadata">

**Author:** ![Jose\_Ionic](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jose\_Ionic](https://forum.ionicframework.com/u/Jose_Ionic)\
**Post date:** [June 30, 2016, 4:22pm UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/11 "2016-06-30T16:22:43Z")

</div>

Is correct, your command work fine but when excecute the next one “typings install dt~socket.io-client --save --global” It appears on the terminal - (No dependencies)  
Do you know why?

---

<div class="post-metadata">

**Author:** ![matheo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/matheo/32/91507_2.png) [@matheo](https://forum.ionicframework.com/u/matheo)\
**Post date:** [June 30, 2016, 4:32pm UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/12 "2016-06-30T16:32:42Z")

</div>

It’s all ok if no error message, its just a report of the installed definitions.

Check your `typings/globals` folder and there should be the `socket.io-client` ones already 😉

---

<div class="post-metadata">

**Author:** ![Jose\_Ionic](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jose\_Ionic](https://forum.ionicframework.com/u/Jose_Ionic)\
**Post date:** [June 30, 2016, 4:40pm UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/13 "2016-06-30T16:40:05Z")

</div>

OH! You are right 🙂 but then… when I run my proyect, why appears on the terminal the message: “Error: Cannot find module ‘socket.io-client’ from 'C:\Users…”

---

<div class="post-metadata">

**Author:** ![matheo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/matheo/32/91507_2.png) [@matheo](https://forum.ionicframework.com/u/matheo)\
**Post date:** [June 30, 2016, 4:42pm UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/14 "2016-06-30T16:42:52Z")

</div>

I got that error for a while too, dunno if it’s a temporary linter issue detecting that module into `node_modules/socket.io-client`, because it should be gone after install it with:

```auto
npm install socket.io-client --save

```

---

<div class="post-metadata">

**Author:** ![Jose\_Ionic](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jose\_Ionic](https://forum.ionicframework.com/u/Jose_Ionic)\
**Post date:** [June 30, 2016, 5:14pm UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/15 "2016-06-30T17:14:15Z")

</div>

I have not understood very well, should I eliminate the folder “socket.io-client” and execute the statement again? In your computer work fine now?

---

<div class="post-metadata">

**Author:** ![matheo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/matheo/32/91507_2.png) [@matheo](https://forum.ionicframework.com/u/matheo)\
**Post date:** [June 30, 2016, 5:15pm UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/16 "2016-06-30T17:15:51Z")

</div>

It works fine, you should trust

```auto
ionic serve

```

more than your IDE error messages, maybe its linter doesn’t detect the changes properly.

---

<div class="post-metadata">

**Author:** ![Jose\_Ionic](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jose\_Ionic](https://forum.ionicframework.com/u/Jose_Ionic)\
**Post date:** [June 30, 2016, 5:18pm UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/17 "2016-06-30T17:18:26Z")

</div>

OK matheo, thank you very very very much for all 🙂

---

<div class="post-metadata">

**Author:** ![matheo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/matheo/32/91507_2.png) [@matheo](https://forum.ionicframework.com/u/matheo)\
**Post date:** [June 30, 2016, 5:19pm UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/18 "2016-06-30T17:19:50Z")

</div>

You’re welcome, have fun!

---

<div class="post-metadata">

**Author:** ![beetz12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/beetz12/32/100830_2.png) [@beetz12](https://forum.ionicframework.com/u/beetz12)\
**Post date:** [June 30, 2016, 6:08pm UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/19 "2016-06-30T18:08:55Z")

</div>

Make sure you are in the right repo.

> <https://github.com/beetz12/ionic2-web-chat/blob/master/app/pages/home/home.ts>

---

<div class="post-metadata">

**Author:** ![Jose\_Ionic](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jose\_Ionic](https://forum.ionicframework.com/u/Jose_Ionic)\
**Post date:** [July 1, 2016, 8:44am UTC](https://forum.ionicframework.com/t/socket-io-ionic/56280/20 "2016-07-01T08:44:53Z")

</div>

OH! That great!! Thak you very much!! I think that yesterday Im not in the right repo.  
Now I have the problem Cannot find module “chat-message” and “socket-service”. I can not find the way to solve it.

[Next page](https://forum.ionicframework.com/t/socket-io-ionic/56280.md?page=2)
