# Api/IRC ping / pong question

**URL:** <https://discuss.dev.twitch.com/t/api-irc-ping-pong-question/1556>\
**Category:** Chat and IRC\
**Created:** [February 4, 2015, 8:52pm UTC](https://discuss.dev.twitch.com/t/api-irc-ping-pong-question/1556 "2015-02-04T20:52:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![H3npi](https://avatars.discourse-cdn.com/v4/letter/h/779978/32.png) [@H3npi](https://discuss.dev.twitch.com/u/H3npi)\
**Post date:** [February 4, 2015, 8:52pm UTC](https://discuss.dev.twitch.com/t/api-irc-ping-pong-question/1556/1 "2015-02-04T20:52:10Z")

</div>

Hey ho,  
i am working on a twitch chat statistics bot (in java), and got a small problem with the connection.  
how should the response pong look like? and to which server should it send to?  
atm i am opening a Socket connection to [irc.twitch.tv](http://irc.twitch.tv).

```
socket = new Socket(irc.twitch.tv, 6667);

```

Joining the chat and everything works fine, i just get time out after i received the second PING.  
atm my response pong looks like this:

```
sender.write("PONG tmi.twitch.tv");

```

sender is a BufferedWriter with a OutputstreamWriter bound to the socket:

```
sender = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

```

Anyone knows how i fix this?  
€: i got my informations from here: [https://github.com/justintv/Twitch-API/blob/master/IRC.md](https://github.com/justintv/Twitch-API/blob/master/IRC.md)

kind regards

H3npi

---

<div class="post-metadata">

**Author:** ![livewhiletrue](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/livewhiletrue/32/348_2.png) [@livewhiletrue](https://discuss.dev.twitch.com/u/livewhiletrue)\
**Post date:** [February 4, 2015, 10:34pm UTC](https://discuss.dev.twitch.com/t/api-irc-ping-pong-question/1556/2 "2015-02-04T22:34:47Z")

</div>

Did you flush the BufferedWriter? You also need to append \r\n to signal end of message.

```
sender.write("PONG tmi.twitch.tv\r\n");
sender.flush();

```

Also, this should only be in the event of a PING messages. You need to listen to the socket in a BufferedReader and respond with an appropriate pong message (the same trailing characters as you received). I believe you need to be sending “PONG :tmi.twitch.tv”, but this should be generated from the message received, as in the linked example.

> **[O'Reilly Media - Technology and Business Training](https://www.oreilly.com)**
>
> Gain technology and business knowledge and hone your skills with learning resources created and curated by O'Reilly's experts: live online training, video, books, our platform has content from 200+ of the world's best publishers.

Take notice that the line:

```
if (line.toLowerCase( ).startsWith("PING ")) {

```

Needs to be substituted with

```
if (line.startsWith("PING ")) {

```

---

<div class="post-metadata">

**Author:** ![H3npi](https://avatars.discourse-cdn.com/v4/letter/h/779978/32.png) [@H3npi](https://discuss.dev.twitch.com/u/H3npi)\
**Post date:** [February 5, 2015, 1:41pm UTC](https://discuss.dev.twitch.com/t/api-irc-ping-pong-question/1556/3 "2015-02-05T13:41:01Z")

</div>

Hey,

sender.write(“PONG [tmi.twitch.tv](http://tmi.twitch.tv)\r\n”);

this fixed the issue.  
thank you!  
kind regards

---

<div class="post-metadata">

**Author:** ![livewhiletrue](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/livewhiletrue/32/348_2.png) [@livewhiletrue](https://discuss.dev.twitch.com/u/livewhiletrue)\
**Post date:** [February 5, 2015, 2:07pm UTC](https://discuss.dev.twitch.com/t/api-irc-ping-pong-question/1556/4 "2015-02-05T14:07:41Z")

</div>

Dynamically generating the response would also allow you to gather chat statistics from big events not hosted on [tmi.twitch.tv](http://tmi.twitch.tv). I suggest avoiding literals with something like:

```
"PONG " + theServerAddress + "\r\n"

```

or

```
"PONG " + thePingMessage.substring(5) + "\r\n"

```

This is a good reference for IRC protocols: [https://tools.ietf.org/html/rfc2812#section-3.7.3](https://tools.ietf.org/html/rfc2812#section-3.7.3)

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [February 15, 2015, 2:07pm UTC](https://discuss.dev.twitch.com/t/api-irc-ping-pong-question/1556/5 "2015-02-15T14:07:53Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
