Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Forked from ToeJamson/1.js
Last active October 27, 2018 05:10
Show Gist options
  • Save stephenlb/8030977 to your computer and use it in GitHub Desktop.
Save stephenlb/8030977 to your computer and use it in GitHub Desktop.
Enter Chat and press enter
<div><input id=input placeholder=you-chat-here /></div>
Chat Output
<div id=box></div>
<script src=http://cdn.pubnub.com/pubnub.min.js></script>
<script>(function(){
var pubnub = PUBNUB.init({publish_key:'demo',subscribe_key:'demo',ssl:true});
var box = PUBNUB.$('box'), input = PUBNUB.$('input'), channel = 'chat';
pubnub.subscribe({
channel : channel,
callback : function(text) { box.innerHTML = (''+text).replace( /[<>]/g, '' ) + '<br>' + box.innerHTML }
});
PUBNUB.bind( 'keyup', input, function(e) {
(e.keyCode || e.charCode) === 13 && pubnub.publish({
channel : channel, message : input.value, x : (input.value='')
})
} )
})()</script>
// Send a message
pubnub.publish({ channel : 'chat', message : "hello!" })
// Receive messages
pubnub.subscribe({ channel : 'chat', message : fun })
@stephenlb
Copy link
Author

Build Real-time Chat Apps in 10 Lines of Code

Quickest way to write Real-time Apps on mobile and web.

With PubNub, you can build real-time chat in 10 lines of JavaScript. This is the quickest way to write a real-time chat application on web or mobile.

PubNub’s framework dramatically reduces your coding. Tasks like sending and receiving data take only one function call. And your code instantly updates in web and mobiles apps, for rapid testing.The network infrastructure and scaling is taken care of for you so you spend time building your app, not infrastructure.

See more at: http://www.pubnub.com/blog/build-real-time-chat-10-lines-code/

@webmechanicx
Copy link

I just test and really minimum code base. thanks for sharing

@sudhamab
Copy link

sudhamab commented Apr 4, 2016

This example doesn't require you to specify publish_key or subscribe_key. I am guessing a lot of people around the world must be copy pasting this example to run it. Keeping it running to see if I catch some other peoples' messages as they run it :)

.. this is cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment